I'm trying to reuse ajax
it to make it more mobile in my project. I put it in the following way expecting a response but no.
function _ajax(params,uri,type){
$.ajax({
url: uri,
type: type,
dataType: 'json',
data: {params},
success: function(data){
return data;
}
});
}
He calls it:
var result = _ajax(null,'http://','GET');
console.log(result);
console:
undefined
How can I reuse it, the answers come inJSON
You have to use a callback like this:
and to call her
The " problem " your request is asynchronous and as you pose it you cannot work it.
Is there any solution?
If I list them below:
the return and params just send an object :)
The ajax function
JQuery
performs the request in a wayasíncrona
: something like in the background.In previous versions it
JQuery
allowed to make a requestsincronica
which allowed you to create a function that returns the data returned by the request indicating the propertyasync:false
, but due to the user experience that functionality was obsolete.You can try this with an older version of jQuery that supports such functionality, but it's not recommended.
What is done for now to reuse is what @alanfcm answered you about the use of callback.