jQuery 速记 AJAX 获取请求的异步错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25488915/
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
Async false for shorthand AJAX get request
提问by Stryner
I am using the following ajax get function to check the status of a user. Is it possible to set async: false
with this shorthand method? If not how can this be changed so that I can add async false?
我正在使用以下 ajax get 函数来检查用户的状态。是否可以async: false
使用这种速记方法进行设置?如果不是,如何更改以便我可以添加 async false?
$.get("/submit/checkuser/", function (userStatus) {
if (userStatus == 'Disabled') { // check if user if disabled
} else if (userStatus == 'Deleted') { // check if user if deleted
} else {
};
}).fail(function () {
connectionError();
});
采纳答案by Bob Tate
This might work for you
这可能对你有用
$.ajax({
url : "/submit/checkuser/",
type : "get",
async: false,
success : function(userStatus) {
if (userStatus == 'Disabled') { // check if user if disabled
} else if (userStatus == 'Deleted') { // check if user if deleted
} else {
};
},
error: function() {
connectionError();
}
});
回答by Stryner
The only way to make a synchronous $.get
call is to set ajaxSetup to synchronous:
进行同步$.get
调用的唯一方法是将 ajaxSetup 设置为同步:
jQuery.ajaxSetup({async:false});
This is considered bad form though, since it affects every ajax call made by JQuery on the page. I would suggest just biting the bullet and using the more elaborate jQuery.ajax
syntax.
不过,这被认为是不好的形式,因为它会影响 JQuery 在页面上进行的每个 ajax 调用。我建议只是咬紧牙关并使用更复杂的jQuery.ajax
语法。
See How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?for a fuller discussion.
请参阅如何让 jQuery 执行同步而不是异步的 Ajax 请求?进行更全面的讨论。
回答by Arun Prasad E S
From above though, I use it like this
从上面虽然,我像这样使用它
jQuery.ajaxSetup({async:false});
// my code
jQuery.ajaxSetup({async:true});
回答by Jp Serame
Try this.
尝试这个。
$.get('/your/url/dot/com', { async: false }, function(data){
globalBaseURL = data;
});
回答by Subhash Diwakar
Use jQuery.ajaxSetup({async:false});
because $.get()
is Jquery shorthand method with syntax jQuery.get( url [, data ] [, success ] [, dataType ] )
and to make changes to setting there is jQuery.get( [settings ] )
使用jQuery.ajaxSetup({async:false});
因为$.get()
是带有语法的 Jquery 速记方法jQuery.get( url [, data ] [, success ] [, dataType ] )
并更改设置有jQuery.get( [settings ] )