简体   繁体   中英

universal jquery ajax call function

wondering is there any kind of global (universal) one function for calling ajax function.. for example i have lots of similar call like this:

$.ajax({
    url: some_url_address,
    type: 'post',
    data: $('#form').serialize(),
    dataType: 'html',
    success: function(data){ 
        alert('success');
    }
}); 

and i would like to make it something like

callAjax('my_url', 'post', {some_data}, 'alert("success")');

and in javascript file:

function callAjax(url, type, data, successFunction, errorFunction, beforeFunction){
     $.ajax({
        url: url,
        type: type,
        data: data,
        dataType: 'html',
        beforeSend: function(){
            beforeFunction;
        },
        success: function(){ 
           successFunction;
        }
        error: function(){ 
           errorFunction;
        }
    }); 
}

that in my page from where i'm calling that function it would return results from before, error, success, etc.. :)

Just install webpack by npm install .

And in the input field write data-ajaxify='true'

Also include the npm js and css file.

<input type='test' data-ajaxify='true' data-url='/someurl' data-method='post'> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM