Reputation: 2662
I'm using a function like this
function example(){
var a;
$.ajax({
url:"www.example.com/function",
type: 'POST',
success: function(result){
a=result;
}
});
alert(a);
}
I need the variable outside of ajax function
Here I'm getting result as undefined. How can I get the value there. ?
Upvotes: 0
Views: 271
Reputation: 96
I think you should return the value in the success function itself
function example(){
var a;
$.ajax({
url:"www.example.com/function",
type: 'POST',
success: function(result){
a=result;
alert(a);
return a;
}
});
}
Upvotes: 0
Reputation: 3283
You can wait for the ajax call to return, by specifying async: false
.
function example(){
var a;
$.ajax({
url:"www.example.com/function",
type: 'POST',
async: false,
success: function(result){
a=result;
}
});
alert(a);
}
Upvotes: 8
Reputation: 11359
var a = "I am defined";
$.ajax({
url:"www.example.com/function",
type: 'POST',
success: function(result){
a=result;
alert(a);
}
});
}
a Will be undefined until the success
call back for the ajax
is called.
Upvotes: 4