axios如何获取后台写入请求头得数据呢

登录后台在请求头写入token和用户信息,我如何获取这些头信息

阅读 11.3k
2 个回答

你好,可以这样拿到:

axios.get('xxx')
  .then(function(response) {
    console.log(response.headers);
  });

其中response包含字段有:

{
  // `data` is the response that was provided by the server
  data: {},

  // `status` is the HTTP status code from the server response
  status: 200,

  // `statusText` is the HTTP status message from the server response
  statusText: 'OK',

  // `headers` the headers that the server responded with
  // All header names are lower cased
  headers: {},

  // `config` is the config that was provided to `axios` for the request
  config: {},

  // `request` is the request that generated this response
  // It is the last ClientRequest instance in node.js (in redirects)
  // and an XMLHttpRequest instance the browser
  request: {}
}

你可以登录成功后储存在localStorage或者cookie里
然后设置ajax的默认头 Token
axios.defaults.headers.common['X-Api-AccessToken'] = window.localStorage.getItem('accessToken')

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题