問(wèn)題描述
我正在為應(yīng)用設(shè)置身份驗(yàn)證.在我發(fā)出登錄請(qǐng)求后,會(huì)發(fā)送一個(gè) JSON Web 令牌作為響應(yīng).我可以通過(guò) Ajax 將它附加到標(biāo)題中.問(wèn)題是在登錄后使用 window.location.pathname 重定向時(shí),因?yàn)樗皇?Ajax 請(qǐng)求,它沒(méi)有附加到標(biāo)頭的令牌.我該如何解決這個(gè)問(wèn)題?
I am setting up authentication for an app. After I make a post request to login, a JSON Web Token is sent in response. I am able to attach this to the header via Ajax. The problem is when using window.location.pathname to redirect after login, since it is not an Ajax request it does not have the token attached to the header. How do I get around this?
$.ajaxSetup({
headers: {
'x-access-token': window.localStorage.jwt
}
});
var Auth = {
signup: function () {
console.log('signuppp');
var userSignup = {
username: $('#usernameSignup').val(),
password: $('#passwordSignup').val()
};
console.log(userSignup)
return $.post('/api/users/register', userSignup, function (resp) {
console.log('resp: ',resp);
window.localStorage.setItem('jwt', resp.token);
//does not have x-access-token header
window.location.pathname = '/';
})
},
推薦答案
簡(jiǎn)短的回答是:您不能使用 window.location
設(shè)置 HTTP 標(biāo)頭.
Short answer is: you cannot set HTTP headers using window.location
.
添加Angular 應(yīng)用程序中 window.location.href 的 http 標(biāo)頭
這篇關(guān)于將標(biāo)題添加到 window.location.pathname的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!