簡體   English   中英

通用jquery ajax調用函數

[英]universal jquery ajax call function

想知道是否有任何一種全局(通用)單一函數來調用 ajax 函數.. 例如,我有很多類似的調用:

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

我想讓它像

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

並在 javascript 文件中:

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;
        }
    }); 
}

在我調用該函數的頁面中,它將返回之前的結果、錯誤、成功等。:)

只需通過npm install安裝 webpack。

並在輸入字段中寫入data-ajaxify='true'

還包括 npm js 和 css 文件。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM