最近做了个H5小项目,再次用到了ajax,因此将封装函数进行分享
$(function(){
/**
* ajax封装
* url 发送请求的地址
* data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1}
* dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text
* successfn 成功回调函数
* errorfn 失败回调函数
*/
jQuery.ax=function(type, url, data, successfn, errorfn) {
data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
$.ajax({
type: type,
data: data,
url: URL+url,
// contentType:"application/json",
async:true,
dataType: "json",
success: successfn,
error: function(e){
errorfn(e);
}
});
};
});