带有JQuery的Ajax:200可以,但不能“成功" [英] Ajax with JQuery: 200 ok, but not “success”

查看:82
本文介绍了带有JQuery的Ajax:200可以,但不能“成功"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试跨域请求时,我遇到了同样的问题.情况很奇怪,我的数据在直接在浏览器上直接命中所请求的URL时就被加载了,奇怪的是,如果使用jquery ajax进行了请求,也会加载该数据, 但不在萤火虫控制台上,而是在萤火虫 net 标签上. 控制台打印: 错误数据示例: parsererror {而json数据格式正确且格式正确,请在json验证程序上检查}

I'm facing the same issue, while trying for cross-domain request. Situation is strange, my data is getting loaded while hit the requested url directly on browser, the strange part is, this gets loaded if requested using jquery ajax as well, but not on firebug console, but on firebug net tab. Console prints: Sample of error data: parsererror {while json data is well-formed and formatted, checked on json validators}

readyState: 4 status: 200 responseText: undefined

网络标签将所有数据加载到响应 json 子标签

Net tab loads all the data in response and json sub-tab

我的示例代码是:

function fetchJsonData() {
$.ajax({
    type: 'POST',
    url: 'http://www.meilleurmobile.com/comparateur/resultats-comparateur-json.do',
    data: 'monthDur%5B0%5D=45.75&monthDur%5B1%5D=45.75&monthDur%5B2%5D=45.75&monthDur%5B3%5D=45.75&monthDur%5B4%5D=45.75&monthDur%5B5%5D=45.75&monthDur%5B6%5D=45.75&monthDur%5B7%5D=45.75&monthDur%5B8%5D=45.75&monthDur%5B9%5D=45.75&monthDur%5B10%5D=45.75&monthDur%5B11%5D=45.75&numSms=1000&dataVolume=1000&withoutMobile=-1&commitmentDuration=-1',
    async: false,
    cache: false,
    //contentType: 'application/json; charset=utf-8',
    crossDomain: true,
    dataType: 'jsonp',
    error: function( xhr,err ) {
        console.log( 'Sample of error data:', err );
        console.log("readyState: "+xhr.readyState+"\nstatus: "+xhr.status+"\nresponseText: "+xhr.responseText);
    },
    success: function( data ) {
        if (console && console.log) {
            console.log( 'Sample of data:', data.slice(0,100) );
        }
    }
})
.done(function() { alert("success"); })
.fail(function() { alert("error"); });

}

推荐答案

尝试的跨域XMLHttpRequest请求可能会使您感到愚蠢.在Firefox Web控制台中,URL看起来可以很好地加载,但是主体是一个空字符串.

Attempted cross domain XMLHttpRequest requests may fool you. In Firefox web console, it may look like URL loaded fine, but body is an empty string.

确认服务器支持JsonP.如果您真的不知道这意味着什么,则需要进行查找.这非常重要.

Confirm that the server supports JsonP. If you don't really know what that means, you need to look it up. It is critically important.

jQuery假定JsonP参数将为?callback =".如果不正确,您应该看到以下内容: http://api.jquery.com/jQuery.ajax /

jQuery assumes that the JsonP parameter is going to be "?callback=". If that is not true, you should see this: http://api.jquery.com/jQuery.ajax/

jsonp:覆盖jsonp请求中的回调函数名称.这个值 将使用'callback =?'中的'callback'代替的一部分 网址中的查询字符串.因此{jsonp:'onJSONPLoad'}将导致 'onJSONPLoad =?'传递给服务器.从jQuery 1.5开始,设置 jsonp选项设置为false可防止jQuery添加?callback" URL的字符串或尝试使用"=?"进行转化.在 在这种情况下,您还应该显式设置jsonpCallback设置. 例如,{jsonp:false,jsonpCallback:"callbackName"}

jsonp: Override the callback function name in a jsonp request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

jsonpCallback:指定JSONP请求的回调函数名称.这个值 将使用代替由自动生成的随机名称 jQuery的.最好让jQuery生成一个唯一的名称,因为它会 使管理请求更加容易,并提供回调和错误 处理.您可能需要在启用时指定回调 更好的浏览器缓存GET请求.从jQuery 1.5开始,您还可以 为此功能使用一个函数,在这种情况下, jsonpCallback设置为该函数的返回值.

jsonpCallback: Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.

如果感到困惑,以老式的方式进行操作可能会更容易,并且可以自己在URL上添加带有时间戳的脚本,以避免使用缓存的脚本页面.

If it gets confusing, it may be easier to just do it the old fashioned way and appends the script to the page yourself with a time stamp in the URL to avoid using a cached script page.

顺便说一句,AFAIK,无法将JSONP和POST结合使用. JSONP是解决XMLHttpRequest的相同来源安全策略的一种方法.它要求您将脚本附加到DOM.我认为您无法做到这一点,并且还需要在过程中提交POST变量.

BTW, AFAIK, there is no way to combine JSONP and POST. JSONP is a way of working around the same origin security policy of XMLHttpRequest. It requires you to append a script to the DOM. I don't think you can do that and also submit POST variables as part of the process.

这篇关于带有JQuery的Ajax:200可以,但不能“成功"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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