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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:39:05  来源:igfitidea点击:

$.ajax( { async : false } ) request is still firing asynchronously?

asp.net-mvcajaxjquery

提问by Galilyou

I got a little problem here guys. I'm trying to implement the following scenario:

我在这里遇到了一个小问题,伙计们。我正在尝试实现以下场景:

  1. A user opens the home page and sees a list of other users and clicks to add one to his friend list.
  2. 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.
  1. 用户打开主页并看到其他用户的列表,然后单击将一个添加到他的朋友列表中。
  2. 我向服务器资源发出 Ajax 请求以验证用户是否已登录,如果是,则向另一个服务器资源发出另一个 ajax 请求以将其实际添加到用户的好友列表中。

Sounds simple? Here's what I've done: I created a function isLoggedInthat will issue the first request to the server in order to determine if the user is logged in. I issue this request using jQuery.ajaxmethod. 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 = trueif logged in, and JsonData = falseif 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 isLoggedInalways returns false, and it returns false before the ajax request finishes (because the messagejsonData = trueis displayed after the message "not logged in". I made sure that the request is **NOT** Asynchronous by statingasync: false`!

调用isLoggedIn总是返回false,它返回false before the ajax request finishes (because the messagejsonData = true is displayed after the message "not logged in". I made sure that the request is **NOT** Asynchronous by statingasync: 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 
}