javascript – Reuse ajax

Question:

I am trying to reuse ajax to make it more movable in my project. I put it in the following way waiting for an answer but no.

function _ajax(params,uri,type){
    $.ajax({
        url: uri,
        type: type,
        dataType: 'json',
        data: {params},
        success: function(data){
            return data;
        }
   });
}

He sends him to call:

var result = _ajax(null,'http://','GET');
console.log(result);

console:

undefined

How can I reuse it, the responses come in JSON

Answer:

You have to use a callback like this:

function _ajax(params,uri,type, callback){
    $.ajax({
        url: uri,
        type: type,
        dataType: 'json',
        data: {params},
        success: function(data){
            callback(data);
        }
    });
}

and to call her

var callback = function(data) {
    console.log(data);
};
_ajax(null,'http://','GET', callback);
Scroll to Top