Ajax 与 JQuery:200 正常,但不“成功”;

发布于 2025-01-07 12:33:42 字数 1581 浏览 2 评论 0原文

我在尝试跨域请求时面临同样的问题。情况很奇怪,我的数据正在加载,同时直接在浏览器上点击请求的 url,奇怪的是,如果使用 jquery ajax 请求,也会加载数据, 但不是在 firebug console 上,而是在 firebug net 选项卡上。 控制台打印: 错误数据示例:parsererror {虽然 json 数据格式正确,已在 json 验证器上进行检查}

readyState: 4 status: 200 responseText: undefined

Net 选项卡加载响应json 子选项卡

我的示例代码是:

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"); });

}

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

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

My sample code is:

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"); });

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感情旳空白 2025-01-14 12:33:42

尝试跨域 XMLHttpRequest 请求可能会欺骗您。在 Firefox Web 控制台中,它可能看起来 URL 加载正常,但正文是一个空字符串。

确认服务器支持JsonP。如果你真的不知道这意味着什么,你需要查一下。这非常重要。

jQuery 假定 JsonP 参数为“?callback=”。如果情况并非如此,您应该看到以下内容: http://api.jquery.com/jQuery.ajax /

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

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

如果它变得令人困惑,那么使用老式方法可能会更容易,并自行将脚本附加到页面,并在 URL 中添加时间戳,以避免使用缓存的脚本页面。

顺便说一句,据我所知,没有办法将 JSONP 和 POST 结合起来。 JSONP 是一种解决 XMLHttpRequest 同源安全策略的方法。它要求您将脚本附加到 DOM。我认为您不能做到这一点,并且还可以在过程中提交 POST 变量。

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.

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 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: 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: 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.

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.

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.

仲春光 2025-01-14 12:33:42

尝试

 console.log(data);

不加切片。

或者,使用 JSON.stringify() (记录在此处)方法更好,

 console.log(JSON.stringify(data.slice(0,100)));

您会得到一个字符串,如下所示未解析或检查 json 有效性。

希望这有帮助。

try with

 console.log(data);

with no slices.

or, even better

 console.log(JSON.stringify(data.slice(0,100)));

with JSON.stringify() (documented here) method you get a string wich is not parsed or checked for json validity.

hope this helps.

遗忘曾经 2025-01-14 12:33:42
function fetchJsonData() {
$.ajax({
    type: 'POST',
    url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%20%3D%20'www.meilleurmobile.com%2Fcomparateur%2Fresultats-comparateur-json.do%3FmonthDur%255B0%255D%3D45.75%26monthDur%255B1%255D%3D45.75%26monthDur%255B2%255D%3D45.75%26monthDur%255B3%255D%3D45.75%26monthDur%255B4%255D%3D45.75%26monthDur%255B5%255D%3D45.75%26monthDur%255B6%255D%3D45.75%26monthDur%255B7%255D%3D45.75%26monthDur%255B8%255D%3D45.75%26monthDur%255B9%255D%3D45.75%26monthDur%255B10%255D%3D45.75%26monthDur%255B11%255D%3D45.75%26numSms%3D1000%26dataVolume%3D1000%26withoutMobile%3D-1%26commitmentDuration%3D-1%26_%3D1329825461536'&format=json&diagnostics=true",
    async: false,
    cache: false,
    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, status ) {
        if (console && console.log) {
            console.log( 'data count:', data.query.results.json.json.length );
            $('#result-count').text( JSON.stringify(data.query.results.json.json) );
        }
    },
    jsonpCallback: 'swatCallback'
})
.done(function() { alert("success"); })
.fail(function() { alert("error"); }); 
}


window['swatCallback'] = function(data) {
    var info = data.query.results.json.json;
    $('#callback_result_operator').html(info[0].operator);
};
function fetchJsonData() {
$.ajax({
    type: 'POST',
    url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%20%3D%20'www.meilleurmobile.com%2Fcomparateur%2Fresultats-comparateur-json.do%3FmonthDur%255B0%255D%3D45.75%26monthDur%255B1%255D%3D45.75%26monthDur%255B2%255D%3D45.75%26monthDur%255B3%255D%3D45.75%26monthDur%255B4%255D%3D45.75%26monthDur%255B5%255D%3D45.75%26monthDur%255B6%255D%3D45.75%26monthDur%255B7%255D%3D45.75%26monthDur%255B8%255D%3D45.75%26monthDur%255B9%255D%3D45.75%26monthDur%255B10%255D%3D45.75%26monthDur%255B11%255D%3D45.75%26numSms%3D1000%26dataVolume%3D1000%26withoutMobile%3D-1%26commitmentDuration%3D-1%26_%3D1329825461536'&format=json&diagnostics=true",
    async: false,
    cache: false,
    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, status ) {
        if (console && console.log) {
            console.log( 'data count:', data.query.results.json.json.length );
            $('#result-count').text( JSON.stringify(data.query.results.json.json) );
        }
    },
    jsonpCallback: 'swatCallback'
})
.done(function() { alert("success"); })
.fail(function() { alert("error"); }); 
}


window['swatCallback'] = function(data) {
    var info = data.query.results.json.json;
    $('#callback_result_operator').html(info[0].operator);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文