gpt4 book ai didi

javascript - 将标题添加到 window.location.pathname

转载 作者:可可西里 更新时间:2023-11-01 02:45:27 25 4
gpt4 key购买 nike

我正在为应用程序设置身份验证。在我发出登录后请求后,将发送一个 JSON Web token 作为响应。我可以通过 Ajax 将其附加到 header 。问题是在登录后使用 window.location.pathname 重定向时,因为它不是 Ajax 请求,所以它没有附加到 header 的 token 。我该如何解决这个问题?

$.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 header 。

Adding http headers to window.location.href in Angular app

关于javascript - 将标题添加到 window.location.pathname,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35966526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com