//封装请求
function push(url,headNum,status,datas){
let headsOne = {'Content-Type':'application/json'};
let headsTwo = {'Content-Type':'application/x-www-form-urlencoded'};
return new Promise((resolve, reject) => {
$.ajax({
url:"http://www.ceshi.com/" + url,
headers:headNum == 0 ? headsOne : headsTwo,
type:status,
data:JSON.stringify(datas),
success:function(res){
document.write(res);
resolve(res);
},
file:function(err){
alert("操作失败,请检查网络");
document.write(err);
resolve(err);
}
});
})
}
调用
let datas = {
name: '',
sex: 2
}
push("xxx/xxxx",0,"POST",datas).then(res =>{
document.write(res)
},err =>{
document.write(err)
});