【问题标题】:AXIOS : Send Authorization header returns Error 401, Authorization Header missingAXIOS:发送授权标头返回错误 401,缺少授权标头
【发布时间】:2017-11-23 21:15:13
【问题描述】:

我找到了this 解决方案,但这对我不起作用。

以下是我的代码:

axios.post('http://myurl/api/login', {
  email: 'john@doe.com',
  password: '123456'
}, {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  }
}).then(response => {
  if (response.data) {
    this.AuthToken = response.data.token
    console.log(this.AuthToken)
    axios.get('http://myurl/userdetails/:uid', {
      uid: 'D123'
    }, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': this.AuthToken
      }
    }).then(response => {
      if (response.data) {
        // this.AuthToken = response.data
        console.log(response.data)
      }
    }).catch(error => {
      console.log('User Data response Error: ' + error)
    })
  }
}).catch(error => {
  console.log('Login Error: ' + error)
})

我从第一个 POST 登录 API 调用中获取令牌。我使用该令牌作为身份验证令牌传递给另一个 API 调用。但我收到错误:缺少授权标头

【问题讨论】:

标签: vue.js authorization axios


【解决方案1】:

找到解决办法:

axios.defaults.headers.common['Authorization'] = this.AuthToken;

【讨论】:

  • 别忘了,你可能需要类似:axios.defaults.headers.common['Authorization'] = 'Basic' + this.authToken;
【解决方案2】:

尝试添加另一个标题。 “访问控制允许标题”:“*”。

【讨论】:

    猜你喜欢
    • 2012-10-27
    • 2019-05-30
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2018-12-28
    • 1970-01-01
    相关资源
    最近更新 更多