为什么 jQuery 不调用我的 Ajax 错误处理程序?

发布于 2024-10-06 20:10:12 字数 695 浏览 1 评论 0原文

我有以下 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.
  1. 为什么我的错误处理程序不会被调用?
  2. 控制台记录的 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.
  1. Why won't my error handler get called?
  2. What is the meaning of the Origin message logged to the console?

Thanks!

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

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

发布评论

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

评论(2

森林散布 2024-10-13 20:10:12

看起来唯一的方法是在我的 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 for complete (which always gets called). No response means the request failed.

It's a shoddy way of handling errors in this otherwise excellent libary.

时光无声 2024-10-13 20:10:12

您的错误处理程序不会被调用,因为 AJAX 请求甚至没有发生:浏览器拒绝执行此操作,因为 同源政策:您请求的数据将来自与为您的网页提供服务的网站不同的网站。

您可以尝试使用 JSONP 数据类型来解决该问题:

$.ajax({ 
    url: "http://www.blah.com/url/does/not/exist", 
    type: "get",
    data: someData,
    dataType: "jsonp",
    error: function(xhr, msg, ex) {
        alert("Failed: " + msg);
    },
    complete: function(xhr, msg) {
       alert("Done: " + msg);
    }
});

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:

$.ajax({ 
    url: "http://www.blah.com/url/does/not/exist", 
    type: "get",
    data: someData,
    dataType: "jsonp",
    error: function(xhr, msg, ex) {
        alert("Failed: " + msg);
    },
    complete: function(xhr, msg) {
       alert("Done: " + msg);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文