开发者

jQuery Ajax response and variable assignment problem

开发者 https://www.devze.com 2023-03-28 19:31 出处:网络
I have the following code inside $(document).ready block: $.ajax({ type: \"POST\", url: \"Test.aspx/CheckType\",

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!!
}

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

I checked with FireBug that the ajax call is successful.

What can be the problem?

Thanks in advance!!!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消