【问题标题】:Add Header to window.location.pathname将标题添加到 window.location.pathname
【发布时间】:2016-06-28 06:01:30
【问题描述】:

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

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

【问题讨论】:

    标签: javascript jquery ajax authentication jwt


    【解决方案1】:

    简短的回答是:您不能使用 window.location 设置 HTTP 标头。

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

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      相关资源
      最近更新 更多