2 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
token可以放到cookie發(fā)給后臺(tái)啊,后臺(tái)能拿到cookie中的token字段,畢竟前端請(qǐng)求也是這么傳過(guò)去的
var div = document.getElementById('div');
div.onclick = () => {
let headres = {
method: 'post',
credentials: 'include',
headers: {
'token': '11111'
},
body: ''
}
fetch('/token', headres).then(x => x.json()).then(x => {
console.log(x);
}).catch(err => {
console.error(err);
})
}
我使用你的發(fā)送方式
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () { //響應(yīng)完成后的回調(diào)函數(shù)
if (xhr.readyState == 4) { //如果響應(yīng)完成...此時(shí)還不知是否響應(yīng)成功
if (xhr.status == 200) { //如果響應(yīng)成功
console.log(xhr.responseText); //使用返回的數(shù)據(jù)responseText
} else {
console.log(xhr.responseText);
}
}
};
xhr.open("POST", '/token'); //準(zhǔn)備請(qǐng)求, 但不發(fā)送. 使用get方法, 獲取a.html
xhr.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');
xhr.setRequestHeader('token', '3333');
xhr.send(222);
添加回答
舉報(bào)