在vuejs应用中运行时axios响应标头缺少数据 [英] axios response headers missing data when running in vuejs app
本文介绍了在vuejs应用中运行时axios响应标头缺少数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有简单的vuejs应用程序.在main.js中,我有类似的内容:
从"vue"导入Vue;从"./App.vue"导入应用;从"./router/routes"导入路由器;从"./store/root"导入商店;从"./plugins/vuetify"导入vuetify;从"./constants"导入{RootActions};从"axios"导入axios;axios.get("https://api.github.com/users/mzabriskie").then(function(response){console.log({headers:response.headers});});
在chrome控制台日志中,我得到了这些:
但是在
问题:
- 为什么在vuejs中运行的axios与纯nodejs环境之间有如此大的差异?
- 我真正想要的是在我的VueJs应用程序中获取响应的
Authorization
标头,这真的可以实现吗?(请注意,我已经在对预检请求的响应的Access-Control-Expose-Headers
中放入了Authorization
引用:
解决方案
好的,这就是问题所在, Access-Control-Expose-Headers
也必须出现在响应的标题中对非战前要求.将标头暴露给所有响应之后,我可以在vuejs应用中访问 Authorization
标头.
I have simple vuejs application. In the main.js I have something like:
import Vue from "vue";
import App from "./App.vue";
import router from "./router/routes";
import store from "./store/root";
import vuetify from "./plugins/vuetify";
import { RootActions } from "./constants";
import axios from "axios";
axios.get("https://api.github.com/users/mzabriskie").then(function(response) {
console.log({ headers: response.headers });
});
In the chrome console log I got these:
However in https://runkit.com/greenlaw110/5e92363de9be35001ab0481e with exactly the same code, I have much more headers printed out:
Question:
- Why there is such a big difference between axios running in vuejs and a pure nodejs environment?
- What I really want is to get the
Authorization
header of the response in my VueJs application, is this really doable in any way? (Note I have already putAuthorization
in theAccess-Control-Expose-Headers
of the response to preflight request
Refer:
- https://github.com/axios/axios/issues/606
- https://github.com/axios/axios/issues/771
- Axios get access to response header fields
解决方案
All right, so here is the problem, the Access-Control-Expose-Headers
must also be presented in the headers of response to non prefight reqeust. After I exposed this headers to all response, I can get access to the Authorization
header in my vuejs app.
这篇关于在vuejs应用中运行时axios响应标头缺少数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文