简易的Ajax封装
JQuery
function http(url, params, method) {
return $.ajax({
url: url,
type: method,
data: params,
dataType: 'json', // 数据类型
beforeSend: function (xhr) {
xhr.setRequestHeader("organId:'1333333333'")
},
headers: {
'Content-Type': 'application/json; charset=utf-8'
// Content-Type: application/json : 请求体中的数据会以json字符串的形式发送到后端
// Content-Type: application/x-www-form-urlencoded:请求体中的数据会以普通表单形式(键值对)发送到后端
// Content-Type: multipart/form-data: 它会将请求体的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件。
},
async: true, // async : 设定是否异步 默认值是true 异步执行ajax请求
timeout: 6000, // timeout : 设定事件 单位->毫秒 如果请求时间超过设定的事件 认为请求失败 必须是异步执行
// cache : 设定是否缓存请求结果 默认值是true 缓存请求结果 必须是 get 方式 这个设定才起作用 post方式不会缓存 设定也没结果
// context : 指定执行函数中this的指向
success: function (res) {},
error: function (err) {},
complete: function (com) {}
})
}