AXIOS:发送授权标头返回错误401,缺少授权标头

时间:2017-06-21 06:12:37

标签: vue.js authorization axios

我遇到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 Login API调用中获取令牌。我用它来传递另一个API调用作为身份验证令牌。但是我收到错误:缺少授权标题

2 个答案:

答案 0 :(得分:1)

找到解决方案:

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

答案 1 :(得分:0)

尝试添加另一个标头。 “Access-Control-Allow-Headers”:“*”。

相关问题