How do I return a value from inside of an $.ajax
function?
This is my basic setup:
function something(){
var id = 0;
$.ajax({
'url':'/some/url',
'type':'GET',
'data':{'some':'data'},
'success':function(data){
id = data['id'];
}
});
return id;
}
In addition to using a callback function as others have pointed out, another option would be to change it to a synchronous
request:
function something(){
var id = 0;
$.ajax({
'url':'/some/url',
'type':'GET',
'async':false,
'data':{'some':'data'},
'success':function(data){
id = data['id'];
}
});
return id;
}
Keep in mind that a synchronous call is a blocking call while the request is active.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With