jQuery $ .ajax使用JSONP调用跨域,返回状态200,但是错误函数被调用而不是成功 [英] jQuery $.ajax call for cross domain with JSONP, returns status 200, but error function is being called instead of success

查看:179
本文介绍了jQuery $ .ajax使用JSONP调用跨域,返回状态200,但是错误函数被调用而不是成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序正在尝试从属于不同域的另一台服务器访问信息.由于这是一个跨域的ajax调用,因此我将"dataType"用作"JSONP"而不是JSON.

My web application is trying to access the information from another server belonging to a different domain. Since it is a cross domain ajax call, i have used "dataType" as "JSONP" instead of JSON.

现在,在WebInspector->网络"选项卡中,我看到请求成功并且响应已填充.但是,从未调用success()函数.它总是以状态= 200调用错误函数.

Now in the WebInspector-> Network Tab, I see the request is successful and the response is populated. But the success() function is never called. It always calls the error function with status = 200.

由于这是JSONP请求,因此jQuery会附加"callback =?"到URL的末尾,即使获得了有效的响应,也从未调用过回调方法.不知道出了什么问题吗?

Since it is a JSONP request, jQuery appends "callback=?" to the end of the URL and the callback method was never called, even after getting the valid response. Not sure what went wrong?

$(document).ready(function() {
    // When you click the Get Instances button:
    $('#getInstances').click(function(){
        $.ajax({
            type: "GET",
            url: "http://178.28.167.221:8080/us-east-1/instance/list.json",
            contentType: "application/json; charset=utf-8",
            accept: "application/json",
            crossDomain: "true",
            headers: {'Access-Control-Allow-Origin','*'},
            dataType: "jsonp",
            success: function(msg) {
                result = JSON.stringify(msg);
                var obj = $.parseJSON(result);
                console.log(obj);
                $('#instancesTable').append('<tr><td>' + obj.autoScalingGroupName + '</td><td>' + obj.amiId + '</td><td>' + obj.instanceId + '</td><td>' + obj.instanceType +'</td></tr>');                                     
            },
            error: function(xhr, ajaxOptions, thrownError, textStatus, responseText) {
                console.log(name + ' environment failed with error: ' + xhr.status + "  " + thrownError);

                var errorMessage = "Error";
                if (xhr.status == 0) {
                    errorMessage = "Can't Connect";
                }
            }
        });
    });
});

我还通过在ajax请求中包含以下内容来进行验证:

I also verified by including the below in my ajax request:

        jsonp: 'callback',
        jsonpCallback: 'jsonpCallback'

并定义如下的回调函数:

and defining the callback function as below:

       function jsonpCallback(data){
         alert("jsonpCallback");
       }

jsonCallback从未被调用,即使请求成功并且服务器可以响应(显示在WebInspector的网络"选项卡中).

jsonCallback was never called, even if the request is successful and response is available from the server (shown in Network Tab of WebInspector).

来自服务器的响应是一个JSON对象数组. [{JSON1},{JSON2} ..]

The response from the server is an Array of JSON objects. [{JSON1}, {JSON2} ..]

注意:由于响应是JSON对象数组,因此我尝试将contentType更改为"application/javascript".但没有任何效果:(

Note : I tried to change the contentType as "application/javascript" since the response was an array of JSON objects. But nothing worked :(

推荐答案

我遇到了类似的问题,并已解决. 您尚未指定您的服务是什么. 就我而言,这是WCF服务.

I had similar issue and resolved it. you have not specified what your service is. In my case, it was a WCF service.

我必须添加它.

   <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>

并在服务中将绑定配置设置为以上

and set the binding configuration to the above in the service

即:bindingConfiguration="webHttpBindingWithJsonP"

这解决了问题.它们可能是在PHP或其他平台上实现相同目标的类似方法

this resolved the issue. They could be a similar means of achieving the same on PHP or other platforms

这篇关于jQuery $ .ajax使用JSONP调用跨域,返回状态200,但是错误函数被调用而不是成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆