javascript - 有什么方法可以“取消” 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 方法?
解决方案
推荐阅读
- python - 在数字和字符串python之间添加空格
- c - CSAPP:如果我想将一个 8 位数字向左移动 8 个位置会发生什么
- vb.net - 如何将 options.txt 文件中的选项和价格列表读入 vb.net 中的相应数组
- python - Python字符串提取:提取两个预定义字符串之间的部分
- c# - 解决 Veracode 的 CWE-73:“文件名或路径的外部控制”MVC 应用程序
- javascript - 从 url Wordpress 的下拉列表中传递选定的选项
- tensorflow - 执行格式错误:尝试用树莓派将 .proto 文件编译成 .py 文件
- angular - 表单组件外的提交按钮
- javascript - 正则表达式:选择第一个 x 和最后一个字母来替换它们
- c# - 如何使用流利的验证在一个类的另一个 IEnumerable 中验证一个类的 IEnumerable