weixin_33708432 2015-09-29 05:11 采纳率: 0%
浏览 5

无法在ajax中进行比较

//From other function
var check = check_abc(a,b,c);
alert (check);


function check_abc(a,b,c)
{

    $.ajax({
    type: "POST",

    url: "abc.php",

    data: { a: a, 
            b: b,
            c:  c
        },

    success:function(data) { //alert to get the data and make sure it was return ok.
        if(data == "OK")
        {
            window.alert("YEA");
            goOn();
        }
        else
        {
            window.alert("Testing");
            noGood();
        }
    }
});

}

Event though it return "OK" (use alert to check before), but no matter what it never show alert("YEA"), why is it so? The data dont seem to be able to compare

  • 写回答

3条回答 默认 最新

  • weixin_33694620 2015-09-29 05:14
    关注

    That's because you're (correctly) using an asynchronous AJAX call. When you call check_abc(a,b,c), it immediately returns an undefined value, since the response from the server will come later.

    Simplify your calling code to this:

    //From other function
    check_abc(a,b,c);
    

    And inside the success function itself, display the dialog (which will happen when you get a response from the server):

    success:function(data) { //alert to get the data and make sure it was return ok.
        var displayBoolean = date === "OK";
        alert(displayBoolean);
    }
    
    评论

报告相同问题?