//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
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);
}
AJAX is asynchronous that why you can't get value
try like this
success:function(data) { //alert to get the data and make sure it was return ok.
if(data == "OK")
{catchVal("OK");return true; } //tried check == true; still cannot
else
{catchVal("Error");return false;}
}
function catchVal(val){
alert(val);
}
$.ajax({
type: "POST",
dataType: "text",
url: "abc.php",
data: { a: a,
b: b,
c: c
},
success:function(data) {
var result = $.trim(data);
if(result == "OK")
{
window.alert("YEA");
goOn();
}
else
{
window.alert("Testing");
noGood();
}