首页 > 解决方案 > 有什么方法可以“取消” promise.done() 被调用?

问题描述

这个想法是,每当服务器中发生某种情况时,我希望浏览器将 HTTP 调用识别为成功,但仍然不调用 $.ajax 调用返回的延迟对象的 done() 函数。

为了处理这个问题,我正在考虑从我的服务器返回一个自定义 HTTP 代码(2XX,确保不是任何标准 HTTP 成功代码)。此代码将是通用的,并且将由遇到这种情况的所有服务器端调用返回

现在,在我的 javascript 代码中,我想确保如果返回代码是我的自定义 2XX,promise.done 方法不应该是叫。

我猜这样的事情应该可行。

$.ajax({
    /* url parameters here */
    success: function (data, textStatus, jqXHR) {

      /*This code is to handle custom  return code 289
      * This code ensures that if there is an issue in getting data from api side
      * the actual promise.done() method is not called. because that would cause Javascript error*/
      if(jqXHR.status === 289){        
            // Stop promise.done from being executed
            // or override the promise.done function to something harmless  

        };
      }
     },
     error: function (jqXHR, textStatus, errorThrown) {

     },
     complete: function (options, status) {

     },
     timeout: 60000
});



有没有办法以标准方式“取消”被调用的 promise.done 方法?

标签: javascriptjqueryajax

解决方案


推荐阅读