我无法从AJAX调用中捕获特定浏览器记录的错误。抓住这些甚至可能吗?

时间:2022-10-28 13:13:23

I'm trying to break an AJAX call in as many ways as I can to anticipate many specific errors. Using the error callback in jQuery ajax, doesn't give me specific enough information compared to what the browser logs (which I would like to catch). I would receive the same generic information (status of 0 with a statusText of "error") for these two errors from Chrome in the error callback:

我试图以尽可能多的方式打破AJAX调用以预测许多特定错误。在jQuery ajax中使用错误回调,与浏览器日志(我想要捕获)相比,没有给我足够的具体信息。对于错误回调中Chrome的这两个错误,我会收到相同的通用信息(状态为0,statusText为“error”):

  1. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'some url' is therefore not allowed access. The response had HTTP status code 503.

    请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问“某些网址”。响应具有HTTP状态代码503。

  2. GET http://google.com/ net::ERR_INTERNET_DISCONNECTED

    获取http://google.com/net :: ERR_INTERNET_DISCONNECTED

Right now my code is similar to the answer in how can i handle multiple ajax errors in jquery. I would like it to address more specific errors, but I suspect it's not possible to catch the specific errors the browser logs.

现在我的代码类似于如何在jquery中处理多个ajax错误的答案。我希望它能解决更具体的错误,但我怀疑它不可能捕获浏览器记录的特定错误。

Is it even possible to catch these errors?

是否有可能发现这些错误?

Note: Wrapping the call in a try-catch does not work either. Using vanilla XHR doesn't provide specific error information either.

注意:在try-catch中包装调用也不起作用。使用vanilla XHR也不提供特定的错误信息。

1 个解决方案

#1


0  

That answer refers to handling the .status object, which will only contain a number. If you want to handle error messages, use the .responseText, and either JSON.parse() it or use indexOf() to check on the exceptions you want to use. You can also use the thrownError string:

该答案指的是处理.status对象,该对象只包含一个数字。如果要处理错误消息,请使用.responseText和JSON.parse(),或使用indexOf()检查要使用的异常。您还可以使用thrownError字符串:

$.ajax({
  ...
  error: function(xhr, ajaxOptions, thrownError) {
      console.log("thrownError: " + thrownError);
  }   
});

#1


0  

That answer refers to handling the .status object, which will only contain a number. If you want to handle error messages, use the .responseText, and either JSON.parse() it or use indexOf() to check on the exceptions you want to use. You can also use the thrownError string:

该答案指的是处理.status对象,该对象只包含一个数字。如果要处理错误消息,请使用.responseText和JSON.parse(),或使用indexOf()检查要使用的异常。您还可以使用thrownError字符串:

$.ajax({
  ...
  error: function(xhr, ajaxOptions, thrownError) {
      console.log("thrownError: " + thrownError);
  }   
});