$check = "";
$.ajax({
url: "check.php",
type: "POST",
cache: false,
data: $name + "=" + $value,
success: function(data){
$check = data;
alert($check);
}
});
alert($check);
The first msg is “Hello”
The second msg is “” Why ?
$.ajaxis an asynchronous function, it returns immediately and calls thesuccesscallback when the success response is received.Therefore, When the
alert($check)at the bottom of your code is executed, the value of$checkis not yet modified by the callback ofajax. That’s why you see the empty string.