ajax方法封装

/**
* 公用方法:使用AJAX获取数据
* @param {[type]} param [参数]
* @param {[type]} url [路径]
* @param {[type]} callBack [成功的回调函数]
*/
function ajax(param, url, callBack) {
  $.ajax({
    type: "GET",
    url: serverUrl + url,
    data: $.param(param, true),
    success: callBack,
    error: function(xhr) {
      console.error("ERROR:" + url + ", xhr.status:" + xhr.status + ", xhr.statusText:" + xhr.statusText);
    }
  });
}
/**
* 公用方法:使用AJAX提交数据
* @param {[type]} param [参数]
* @param {[type]} url [路径]
* @param {[type]} callBack [成功的回调函数]
*/
function ajaxPost(param, url, callBack) {
  $.ajax({
    type: "POST",
    url: serverUrl + url,
    data: $.param(param, true),
    success: callBack,
    error: function(xhr) {
    console.error("ERROR:" + url + ", xhr.status:" + xhr.status + ", xhr.statusText:" + xhr.statusText);
    }
  });
}

原文地址:https://www.cnblogs.com/huliang56/p/6437414.html