Ajax 与 JQuery:200 正常,但不“成功”;
我在尝试跨域请求时面临同样的问题。情况很奇怪,我的数据正在加载,同时直接在浏览器上点击请求的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试跨域 XMLHttpRequest 请求可能会欺骗您。在 Firefox Web 控制台中,它可能看起来 URL 加载正常,但正文是一个空字符串。
确认服务器支持JsonP。如果你真的不知道这意味着什么,你需要查一下。这非常重要。
jQuery 假定 JsonP 参数为“?callback=”。如果情况并非如此,您应该看到以下内容: http://api.jquery.com/jQuery.ajax /
如果它变得令人困惑,那么使用老式方法可能会更容易,并自行将脚本附加到页面,并在 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/
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.
尝试
不加切片。
或者,使用 JSON.stringify() (记录在此处)方法更好,
您会得到一个字符串,如下所示未解析或检查 json 有效性。
希望这有帮助。
try with
with no slices.
or, even better
with JSON.stringify() (documented here) method you get a string wich is not parsed or checked for json validity.
hope this helps.