将标题添加到window.location.pathname [英] Add Header to window.location.pathname

查看:494
本文介绍了将标题添加到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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆