1. 程式人生 > >jquery.ajax post/get/delete/put 請求方法封裝

jquery.ajax post/get/delete/put 請求方法封裝

ajax中的post/get/delete/put請求方法的寫法過於繁瑣,所以現在封裝成如下簡便的形式:

 /**
     * 獲取資料ajax-get請求
     * @author laixm
     */
    $.sanjiGetJSON = function (url,data,callback){
        $.ajax({
            url:url,
            type:"get",
            contentType:"application/json",
            dataType:"json",
            timeout:10000
, data:data, success:function(data){ callback(data); } }); }; /** * 提交json資料的post請求 * @author laixm */ $.postJSON = function(url,data,callback){ $.ajax({ url:url, type:"post", contentType:"application/json"
, dataType:"json", data:data, timeout:60000, success:function(msg){ callback(msg); }, error:function(xhr,textstatus,thrown){ } }); }; /** * 修改資料的ajax-put請求 * @author laixm */
$.putJSON = function(url,data,callback){ $.ajax({ url:url, type:"put", contentType:"application/json", dataType:"json", data:data, timeout:20000, success:function(msg){ callback(msg); }, error:function(xhr,textstatus,thrown){ } }); }; /** * 刪除資料的ajax-delete請求 * @author laixm */ $.deleteJSON = function(url,data,callback){ $.ajax({ url:url, type:"delete", contentType:"application/json", dataType:"json", data:data, success:function(msg){ callback(msg); }, error:function(xhr,textstatus,thrown){ } }); };