jQuery $.ajax( { async : false } ) 请求仍在异步触发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1531693/
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
$.ajax( { async : false } ) request is still firing asynchronously?
提问by Galilyou
I got a little problem here guys. I'm trying to implement the following scenario:
我在这里遇到了一个小问题,伙计们。我正在尝试实现以下场景:
- A user opens the home page and sees a list of other users and clicks to add one to his friend list.
- I issue an Ajax request to a server resource to validate if the user is logged in, if so, I issue another ajax request to another server resource to actually add it to the user's friend list.
- 用户打开主页并看到其他用户的列表,然后单击将一个添加到他的朋友列表中。
- 我向服务器资源发出 Ajax 请求以验证用户是否已登录,如果是,则向另一个服务器资源发出另一个 ajax 请求以将其实际添加到用户的好友列表中。
Sounds simple? Here's what I've done: I created a function isLoggedIn
that will issue the first request to the server in order to determine if the user is logged in. I issue this request using jQuery.ajax
method. Here's my function looks like:
听起来很简单?这是我所做的:我创建了一个函数isLoggedIn
,该函数将向服务器发出第一个请求,以确定用户是否已登录。我使用jQuery.ajax
方法发出此请求。这是我的功能:
function isLoggedIn() {
$.ajax({
async: "false",
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "/isloggedin",
success: function(jsonData) {
alert("jsonData =" + jsonData.LoggedIn);
return jsonData.LoggedIn;
}
});
}
The returned JSON is very simple, it looks like the following:
返回的JSON非常简单,如下所示:
{ LoggedIn: true } or { LoggedIn : false }
Now this method, actually works and displays the alert correctly: JsonData = true
if logged in, and JsonData = false
if not logged in.
Up to this point there's no problem, the problem occurs when I try to call this method: I call it like so:
现在这个方法,实际工作并正确显示警报:JsonData = true
如果登录,JsonData = false
如果没有登录。 到目前为止没有问题,当我尝试调用这个方法时出现问题:我这样调用它:
$(".friend_set .img").click(function() {
debugger;
if (isLoggedIn()) {
alert("alredy logged in");
trackAsync();
popupNum = 6;
}
else {
alert("not logged in"); //always displays this message.
popupNum = 1;
}
//centering with css
centerPopup(popupNum);
//load popup
loadPopup(popupNum);
return false;
});
Calling isLoggedIn
always returns false
, and it returns false before the ajax request finishes (because the message
jsonData = trueis displayed after the message "not logged in". I made sure that the request is **NOT** Asynchronous by stating
async: false`!
调用isLoggedIn
总是返回false
,它返回false before the ajax request finishes (because the message
jsonData = true is displayed after the message "not logged in". I made sure that the request is **NOT** Asynchronous by stating
async: false`!
Apparently it's still working asynchronously, though. What am I missing here guys?
不过,显然它仍在异步工作。伙计们,我在这里错过了什么?
回答by Roatin Marth
You need async:false
, not async:"false"
. (i.e. pass boolean false
, not string "false"
).
你需要async:false
,不是async:"false"
。(即传递 boolean false
,而不是 string "false"
)。
Edit:
also with async requests you need to return a value afterthe call to ajax
, notinside your success handler:
编辑:对于异步请求,您需要在调用后返回一个值ajax
,而不是在您的成功处理程序中:
function isLoggedIn() {
var isLoggedIn;
$.ajax({
async: false,
// ...
success: function(jsonData) {
isLoggedIn = jsonData.LoggedIn
}
});
return isLoggedIn
}