代码如下(主要关键就是headers,大家可以根据需要来增加请求头):
$.ajax({ type: "POST", timeout: 10000, // 超时时间 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=="111111"){ layui.use(\'layer\', function(){ var layer = layui.layer; layer.msg(data.msg,{icon:1}); }); }else if(data.code=="000000"){ layui.use(\'layer\', function(){ var layer = layui.layer; layer.msg(data.msg,{icon:1}); }); setTimeout(() => { closeLayui(); }, 700); $("#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) { //请求完成后最终执行参数 } }) }