jQuery Ajax响应和变量分配问题 [英] jQuery Ajax response and variable assignment problem

查看:86
本文介绍了jQuery Ajax响应和变量分配问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在$(document).ready块中有以下代码:

I have the following code inside $(document).ready block:

$.ajax({
    type: "POST",
    url: "Test.aspx/CheckType",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        if (response.d == true) {
            user = true;   
        }
        else {
            user = false;   
        }
    },
    error: function () {
        alert("ERROR");  
    }
});


if (user) {
    // code that doesn't execute
}
else {
    // code that always executes!!
}

问题在于用户变量未定义,并且始终在else块内执行代码.

The problem is that user variable is undefined and always executes the code inside the else block.

我用FireBug检查了ajax调用是否成功.

I checked with FireBug that the ajax call is successful.

可能是什么问题?

提前谢谢!

推荐答案

最有可能是在返回ajax调用之前读取了用户变量的问题,因此它始终是未定义的,请尝试将if语句放在ajax调用中并以这种方式进行测试.

It's most likely a problem with the user variable being read before the ajax call is returned so it will always be undefined, try placing your if statement inside the ajax call and test it that way.

这篇关于jQuery Ajax响应和变量分配问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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