【发布时间】:2017-08-31 17:58:51
【问题描述】:
当我实例化一个 Vuejs (2.2.6) 和 Vue-resource (1.2.1) 时,我使用以下代码设置标题授权,这样我可以授权对我的 API 的所有请求:
Vue.http.headers.common.AUTHORIZATION = 'BEARER ...';
但是,我想请求第三方 API,并且我不希望发送 Authorization 字段。此外,此 API 不允许您使用此授权标头。
let CEP = '';
this.$http.get('https://viacep.com.br/ws/' + CEP + '/json')
.then(response => {
console.log(response.headers);
});
这样,授权字段与标头一起发送,在 Access-Control-Request-Headers 上:
我尝试使用以下代码删除一些标题字段,但没有成功。
this.$http.headers.common.AUTHORIZATION = null;
this.$http.headers.common['Access-Control-Allow-Headers'] = null;
this.$http.get('https://viacep.com.br/ws/' + CEP + '/json')
.then(response => {
console.log(response.headers);
});
在vue-resource 文档中,有可能插入一个对象来强制请求配置,但文档并不完整。
this.$http.get('https://viacep.com.br/ws/' + CEP + '/json', {
...here...
}).then(response => {
console.log(response.headers);
});
有没有办法从给定的请求中删除 Authorization 字段或任何其他字段?
谢谢。
* 已更新 *
通过使用拦截器(如下例所示)我可以编辑请求,但我不能删除特定字段。
Vue.http.interceptors.push((request, next) => {
const viacep = request.url.includes('viacep.com.br');
if (viacep) {
request.headers.set('AUTHORIZATION', 'TRY THIS');
}
next(response => {});
});
尝试删除:
Vue.http.interceptors.push((request, next) => {
const viacep = request.url.includes('viacep.com.br');
if (viacep) {
request.headers.delete('AUTHORIZATION');
}
next(response => {});
});
【问题讨论】:
标签: javascript vue.js vuejs2 vue-resource