Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  27] [ 6]  / answers: 1 / hits: 51449  / 8 Years ago, sun, march 13, 2016, 12:00:00

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 = '/';
})
},




More From » jquery

 Answers
9

Short answer is: you cannot set HTTP headers using window.location.



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


[#62957] Thursday, March 10, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dakotahs

Total Points: 605
Total Questions: 104
Total Answers: 113

Location: Hungary
Member since Wed, Nov 9, 2022
2 Years ago
;