The only way to retrieve the correct value in your context is to run $.ajax()
function synchronously (what actually contradicts to main AJAX idea). There is the special configuration attribute async
you should set to false
. In that case the main scope which actually contains $.ajax()
function call is paused until the synchronous function is done, so, the return
is called only after $.ajax()
.
function doSomething() {
var status = 0;
$.ajax({
url: 'action.php',
type: 'POST',
data: dataString,
async: false,
success: function (txtBack) {
if (txtBack == 1)
status = 1;
}
});
return status;
}
var response = doSomething();