我想捕捉在页面停止所有Ajax请求/放弃一些依赖于一些检查,我们做了要求。最初的 jqXHR.abort(); 的作品,但问题是,它是执行所有我们中止请求错误事件,这是我不希望
I want to capture all ajax request on a page stop/abort some of the request depending on the some checking we do. Initially jqXHR.abort(); works but the problem is that it is executing error event of all the request we aborted, which I do not want.
是我创建的示例代码段是像下面。
A sample snippet of what I created is like below.
var newajaxOptions;
$( ":button" ).on("click", function () {
var URL = "#";
$.ajax({
url: URL,
type: 'GET',
async: false,
success: function(){
alert("AJAX SUCCESS");
},
error: function(){
alert("AJAX ERROR");
}
});
});
$(document).ajaxSend(function(event, jqXHR, ajaxOptions) {
alert("BLOCKED");
//set options to global variable so can re initiate ajax call later
newajaxOptions = ajaxOptions;
jqXHR.abort();
});
PS作为一个限制,我不能做任何修改Ajax请求,所以我只能拦截在细节上的所有请求检查停止那些不继续进行。
P.S As a limitation I am not allowed to make any modification to the ajax request so I can only intercept the all the request check on the details stop those which are not allowed to proceed.
jqXHR可以真正的帮助
jqXHR can be really helpful
if(jqXHR.aborted)
return;
和脚本:
var newajaxOptions;
$( ":button" ).on("click", function () {
var URL = "#";
$.ajax({
url: URL,
type: 'GET',
async: false,
success: function(){
alert("AJAX SUCCESS");
},
error: function(jqXHR, textStatus, errorThrown){
if(jqXHR.aborted)
return;
alert("AJAX ERROR");
}
});
});
$( document ).ajaxSend(function(event, jqXHR, ajaxOptions) {
alert("BLOCKED");
//set options to global variable so can re initiate ajax call later
newajaxOptions = ajaxOptions;
jqXHR.abort();
});
var newajaxOptions;
$( ":button" ).on("click", function () {
var URL = "#";
$.ajax({
url: URL,
type: 'GET',
async: false,
success: function(){
alert("AJAX SUCCESS");
},
error: function(jqXHR, textStatus, errorThrown){
//if(jqXHR.aborted)
// return;
alert("AJAX ERROR");
}
});
alert("After Button Click");
});
$( document ).ajaxSend(function(event, jqXHR, ajaxOptions) {
alert("BLOCKED");
throw "Your request is evil! Stop right now!";
});
这是像在这种情况下,黑客。 投掷
工作在每一个浏览器和副作用是中断当前栈。但要小心警报(按钮后点击);
也是当前堆栈和将永远不执行,当你扔东西
This is something like "hack" in this case. Throw
works in every browser and side effect is interrupt of current stack. But be careful alert("After Button Click");
is also in current stack and will never execute, when you throw something.
更多关于:
掷
声明
错误处理的try ... catch