0

我们有一个错误处理程序来发布消息。

 $(document).ajaxError(function (e, xhr, options) {
  window.postMessage(....);  
});

其中一个 fetch 调用返回 401

this.fetch = function(url) {
return fetch(url, { 
  }
}).then(function(response) {
  return response.json();
})

当我检查响应时,状态为 401,但 response.json() 正在抛出“Uncaught (in promise) SyntaxError: Unexpected end of JSON input。”因此,ajaxError 没有触发。

预期行为:如果我没有从 fetch 调用中获得状态 200,我希望 document.ajaxError 发布消息。

我们有一个 wrapper.js 具有覆盖功能

 window.fetch = (function(arg) {


 }(window.fetch)

我可以在这个函数中全局捕获这个异常吗?

4

1 回答 1

0

根据评论,我无法在 ajaxError 处理程序上执行此操作,因为它不是源自 jquery。我能够在 Window.fetch 包装器中做到这一点

 window.fetch = (function (original)) {
  ...............

  return original.apply(this, args).then(function (response)) {

      if(response.status !== 200)
       {
           window.postMessage({ event_type: 'request:failed', status: response.status); 
       }   
   }
}
于 2019-07-11T12:34:54.000 回答