Yesterday I was working on a small task of validation through jquery ajax, I was comparing two dates through ajax and then needs to set an variable on success, which I can use further in the same function but it was not working as per requirement. My code was like
$.ajax({
type: "POST",
url: 'test.php',
data: datastr,
success: function(res) {
result = res;
}
});
This code was not giving me “result” variable and I want to use it further in next ajax request, then I searched and found as the request is asynchronous it will not work and I made the request synchronous, like
var result = '';
$.ajax({
type: "POST",
async: false,
url: 'test.php',
data: datastr,
success: function(res) {
result = res;
}
});
alert(result); // use it here
Now I can use result any where in this function.




