$阿贾克斯({异步:假})请求仍然射击异步? [英] $.ajax( { async : false } ) request is still firing asynchronously?

查看:264
本文介绍了$阿贾克斯({异步:假})请求仍然射击异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来到这里有点问题家伙。我试图实施以下情形:

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

  1. 在用户打开主页,看到其他用户和点击名单 添加一个到他的好​​友列表。
  2. 在我发出一个Ajax请求到服务器资源,如果用户验证 登录,如果是的话,我发出另一个Ajax请求到另一台服务器 资源实际上它添加到用户的朋友列表中。
  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.

听起来很简单?下面是我做了什么:我创建了一个功能 isLoggedIn 将颁发第一个请求到服务器,以确定用户是否登录我使用发出此请求。 jQuery.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:

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;
        }
    });
}

返回的JSON是非常简单的,它看起来像如下:

The returned JSON is very simple, it looks like the following:

{ LoggedIn: true } or { LoggedIn : false } 

现在这种方法,实际工作并正确显示警告: JsonData = TRUE 如果登录, JsonData = 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:

$(".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;

});

调用 isLoggedIn 总是返回,并返回 Ajax请求完成之前假(因为消息 jsonData = TRUE 没有登录我确信该请求是**不**异步通过声明的消息后,将显示异步: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`!

显然,它仍然以异步方式运行,但。我错过这里的家伙?

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

推荐答案

您需要异步:假,不是异步:假的。 (即通过布尔,不串)。

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

编辑: 还与异步请求,你需要返回一个值的的调用 AJAX 没有的你的成功处理器中:

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

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

这篇关于$阿贾克斯({异步:假})请求仍然射击异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆