ajax如何增加请求头

时间:2021-11-12 08:59:53

代码如下(主要关键就是headers,大家可以根据需要来增加请求头):

     $.ajax({
type: "POST",
timeout: , // 超时时间 10 秒
headers: {
'token': "U2PGBjguSuJg",
           'version': "1.0",
            'from': "test",
},
url: "http://www.yctech.com/api/login",
data:{"username":username,"password":password},
dataType: "json",
success: function(data) { if(data.code==""){
layui.use('layer', function(){
var layer = layui.layer; layer.msg(data.msg,{icon:});
}); }else if(data.code==""){ layui.use('layer', function(){
var layer = layui.layer; layer.msg(data.msg,{icon:});
});
setTimeout(() => { closeLayui();
}, ); $("#login").hide(); $("#exit").show(); $("#user").html(data.data.nickName); } },
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest.status);
console.log(XMLHttpRequest.readyState);
console.log(textStatus);
},
complete: function(XMLHttpRequest, status) { //请求完成后最终执行参数 
}
})
}