I have a js function , after doing some business logic, the javascript function should return some result to another variable.Sample code below
var response="";
function doSomething() {
$.ajax({
url:'action.php',
type: "POST",
data: dataString,
success: function (txtBack) {
if(txtBack==1) {
status=1;
}
});
return status;
}
Here i want to use like
response=doSomething();
I want to assign a return value "status" "var response".But the result is 'undefined'.
return
in your function ;) – Inappreciablereturn
statement is executed before the Ajax call finished. You can solve this using callbacks... – Inappreciableasync: false
is bad. Depending on how long the Ajax call takes, it can freeze the browser. And you loose imho the most important feature of an Ajax call. – Inappreciable