$.ajax应⽤之请求头headers
昨天项⽬中,由于要请求token验证,后台给出的⽅案是采⽤请求头返回token数据,给出的API⽂档是这样的由于之前⼀直都是采⽤请求体发送请求,服务器在应答体李返回数据。和这个不⼀样;
采⽤jq的$.ajax()函数发送请求,代码如下
$.ajax({
type: 'POST',
url: '/token',
headers:{"appId":appId,"appKey":appKey,"Content-Type":"text/plain;charset=UTF-8"},
data:"{}",
success: function(data, status, xhr){
console.log(xhr);
},
error: function(xhr, type){
load();
}
这样写正确,能够正确发送请求,也能正常收到response headers:
使⽤jq的
getAllResponseHeaders()能得到所有response headers,取到的值是str类型;
使⽤
发送ajax请求的步骤
getResponseHeader('token')能得到token的值;