jQuery 在 beforeSend 上停止 $.ajax
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10507079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop $.ajax on beforeSend
提问by Mr_Nizzle
I have this jQuery ajax call:
我有这个 jQuery ajax 调用:
$.ajax({
url : 'my_action',
dataType: 'script',
beforeSend : function(){
if(1 == 1) //just an example
{
return false
}
},
complete: function(){
console.log('DONE');
}
});
I want to stop the ajax call under the beforeSend
if the condition returns true
but return false does not stop the ajax call.
我想beforeSend
在条件返回的情况下停止ajax调用true
但返回false不会停止ajax调用。
How can I stop
the ajax call on the beforeSend
?
我怎样才能stop
在 ajax 上调用beforeSend
?
======= UPDATE =========
======== 更新 ==========
return false
works as well.
return false
也有效。
回答by thecodeparadox
$.ajax({
url : 'my_action',
dataType: 'script',
beforeSend : function(xhr, opts){
if(1 == 1) //just an example
{
xhr.abort();
}
},
complete: function(){
console.log('DONE');
}
});
回答by Farhan Ahmad
Most of the jQuery Ajax methods return an XMLHttpRequest (or the equivalent) object, so you can just use abort().
大多数 jQuery Ajax 方法都返回一个 XMLHttpRequest(或等效的)对象,因此您只需使用 abort()。
var test = $.ajax({
url : 'my_action',
dataType: 'script',
beforeSend : function(){
if(1 == 1) //just an example
{
test.abort();
return false
}
},
complete: function(){
console.log('DONE');
}
});
回答by The Alpha
beforeSend:function(jqXHR,setting)
{
// if(setting.url != "your url") jqXHR.abort();
if(1 == 1) //just an example
{
jqXHR.abort();
}
}
回答by Valmor Nascimento
xhr.done()
;
this works for me
xhr.done()
; 这对我有用
$.ajax({
url : 'my_action',
dataType: 'script',
beforeSend : function(xhr){
if(1 == 1) //just an example
{
return false
};
xhr.done(); //this works for me
},
complete: function(){
console.log('DONE');
}
});
http://api.jquery.com/jquery.ajax/
http://api.jquery.com/jquery.ajax/
jqXHR.done(function( data, textStatus, jqXHR ) {});
An alternative construct to the success callback option, refer to deferred.done()
for implementation details.
成功回调选项的替代构造,请参阅deferred.done()
有关实现的详细信息。