function ajax_test(str1){
var url = "None"
jq.ajax({
type:'post',
cache: false,
url: 'http://....' + str1,
success: function(data, status, xhr){
url=data;
},
error: function (xhr, status, e) {
},
async: true,
dataType: 'json'
});
return url
}
How can I set the global variable url to be the returned success ajax data?
In Javascript, it is impossible for a function to
returnan asynchronous result. The function will usually return before the AJAX request is even made.You can always force your request to be syncronous with
async: false, but that’s usually not a good idea because it will cause the browser to lock up while it waits for the results.The standard way to get around this is by using a callback function.
and then you can call it like this: