为什么 jQuery 不调用我的 Ajax 错误处理程序?
我有以下 jQuery Ajax 调用(没有其他全局设置/处理程序):
$.ajax( {
url: "http://www.blah.com/url/does/not/exist",
type: "get",
data: someData,
dataType: "json",
error: function (xhr, msg, ex)
{
alert("Failed: " + msg);
},
complete: function (xhr, msg)
{
alert("Done: " + msg);
}
我希望调用我的错误处理程序,但会触发 complete
事件,并且警报显示 Done:成功
。在我的 Javascript 控制台中,我看到以下消息:
XMLHttpRequest cannot load http://www.blah.com/url/does/not/exist.
Origin null is not allowed by Access-Control-Allow-Origin.
- 为什么我的错误处理程序不会被调用?
- 控制台记录的
Origin
消息的含义是什么?
谢谢!
I have the following jQuery Ajax call (There are no other global settings/handlers):
$.ajax( {
url: "http://www.blah.com/url/does/not/exist",
type: "get",
data: someData,
dataType: "json",
error: function (xhr, msg, ex)
{
alert("Failed: " + msg);
},
complete: function (xhr, msg)
{
alert("Done: " + msg);
}
I would expect my error handler to be called, but instead the complete
event fires and the alert displays Done: success
. In my Javascript console, I see the following message:
XMLHttpRequest cannot load http://www.blah.com/url/does/not/exist.
Origin null is not allowed by Access-Control-Allow-Origin.
- Why won't my error handler get called?
- What is the meaning of the
Origin
message logged to the console?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来唯一的方法是在我的
success
方法中接收服务器响应并将其存储在全局变量中,我将在处理程序中检查complete
(它总是被叫)。没有响应意味着请求失败。在这个本来很优秀的库中,这是一种处理错误的劣质方法。
Looks like the only way is to receive the server response in my
success
method and store it in a global variable, which I'll check in the handler forcomplete
(which always gets called). No response means the request failed.It's a shoddy way of handling errors in this otherwise excellent libary.
您的错误处理程序不会被调用,因为 AJAX 请求甚至没有发生:浏览器拒绝执行此操作,因为 同源政策:您请求的数据将来自与为您的网页提供服务的网站不同的网站。
您可以尝试使用
JSONP
数据类型来解决该问题:Your error handler isn't called because the AJAX request doesn't even take place: the browser refuses to do that because of the same origin policy: the data you're requesting would come from a different site than the one serving your page.
You can try using the
JSONP
data type to work around the problem: