jQuery post 响应readyState:0,status:0
全部。我使用的是 jquery 版本 1.6.2。 我执行如下 ajax 调用:
var jqxhr = $j.post(myPHP, function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function(data) { alert(JSON.stringify(data)); })
.complete(function() { alert("complete"); });
脚本运行并显示错误,然后完成。有些人可能会认为...myPHP有一些问题,但是myPHP总是显示:
{"sayHi":"hihi"}
所以,我去firebugs检查链接是否有问题,当我调用..: 它显示了状态为 200 的 POST url,这是可以的。另外,我可以通过萤火虫看到响应...... 但问题是......为什么jquery所以我有一个错误......: 这是错误消息:
{"readyState":0,"responseText":"","status":0,"statusText":"error"}
all. I am using a jquery version 1.6.2.
I do a ajax call like this:
var jqxhr = $j.post(myPHP, function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function(data) { alert(JSON.stringify(data)); })
.complete(function() { alert("complete"); });
The script, run, and show error, than, complete. Some people may thing that...myPHP have some problem, but the myPHP is always show:
{"sayHi":"hihi"}
So, I go to firebugs to check whether the link have any problem, when I called..:
It show me the POST url with status 200, which is ok. Also, I can see the respond via firebugs....
But the question is ....why the jquery so me have a error...:
And here is the error msg:
{"readyState":0,"responseText":"","status":0,"statusText":"error"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我来说,这个问题是由跨域问题引起的。
所以我有一个本地 html 文件 (
c:\test.html
) 并尝试从服务器 (localhost/servlet) 获取数据。当将 html 放在服务器上时,问题就消失了。
For me this problem was caused by a cross-domain issue.
So I had a local html file (
c:\test.html
) and tried to get data from a server (localhost/servlet).When putting html on the server - the problem was gone.
如果浏览器在 XHR 请求仍在进行时切换网页(用户单击链接、后退按钮等),则该 XHR 请求将被取消,并显示您的错误。
请查看以下博客文章,其中深入解释了该问题: http://bartwullems.blogspot.de/2012/02/ajax-request-returns-status-0.html
If the browser switches the web page while an XHR request is still in progress (the user clicked a link, the back button, …), this XHR request will be cancelled with exactly your error.
Have a look at the following blog post, where the issue is explained in depth: http://bartwullems.blogspot.de/2012/02/ajax-request-returns-status-0.html
您忘记告诉 jQuery 服务器正在返回 JSON:
You forgot to tell jQuery that the server is returning JSON:
就我而言,这不是由于跨域。我没有使用 e.PreventDefault()。请参阅此处
In my case, it was not due to Cross domain. I was not using e.PreventDefault(). See here