本文介紹了將標題添加到 window.location.pathname的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在為應用設置身份驗證.在我發出登錄請求后,會發送一個 JSON Web 令牌作為響應.我可以通過 Ajax 將它附加到標題中.問題是在登錄后使用 window.location.pathname 重定向時,因為它不是 Ajax 請求,它沒有附加到標頭的令牌.我該如何解決這個問題?
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 = '/';
})
},
推薦答案
簡短的回答是:您不能使用 window.location
設置 HTTP 標頭.
Short answer is: you cannot set HTTP headers using window.location
.
添加Angular 應用程序中 window.location.href 的 http 標頭
這篇關于將標題添加到 window.location.pathname的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!