美元。ajax({异步:false})请求仍然异步地触发?

时间:2022-10-16 20:14:49

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. 用户打开主页,看到其他用户的列表,并单击以将其添加到好友列表中。
  3. 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.
  4. 我向服务器资源发出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 messagejsonData = trueis displayed after the message "not logged in". I made sure that the request is **NOT** Asynchronous by statingasync: false`!

调用isLoggedIn总是返回false,在ajax请求完成之前它返回false(因为messagejsonData = trueis在消息“未登录”后显示)。我通过statingasync: false '确保请求是**而不是**异步的!

Apparently it's still working asynchronously, though. What am I missing here guys?

显然它仍然在异步工作。我错过了什么,伙计们?

1 个解决方案

#1


61  

You need async:false, not async:"false". (i.e. pass boolean false, not string "false").

您需要async:false,而不是async:“false”。(即传递布尔值为false,而不是字符串“false”)。

Edit: also with async requests you need to return a value after the call to ajax, not inside your success handler:

编辑:对于异步请求,您需要在调用ajax之后返回一个值,而不是在您的成功处理程序中:

function isLoggedIn() {
    var isLoggedIn;
    $.ajax({
        async: false,
        // ...
        success: function(jsonData) {
            isLoggedIn = jsonData.LoggedIn
        }
    });
    return isLoggedIn 
}

#1


61  

You need async:false, not async:"false". (i.e. pass boolean false, not string "false").

您需要async:false,而不是async:“false”。(即传递布尔值为false,而不是字符串“false”)。

Edit: also with async requests you need to return a value after the call to ajax, not inside your success handler:

编辑:对于异步请求,您需要在调用ajax之后返回一个值,而不是在您的成功处理程序中:

function isLoggedIn() {
    var isLoggedIn;
    $.ajax({
        async: false,
        // ...
        success: function(jsonData) {
            isLoggedIn = jsonData.LoggedIn
        }
    });
    return isLoggedIn 
}