Thi*_*ira 6 javascript vue.js vue-resource vuejs2
当我实例化Vuejs (2.2.6)和Vue-resource (1.2.1)时,我使用以下代码设置标头授权,这样我就可以授权对我的API的所有请求:
Vue.http.headers.common.AUTHORIZATION = 'BEARER ...';
Run Code Online (Sandbox Code Playgroud)
但是,我想要请求第三方API,我不希望Authorization发送该字段.此外,此API不允许您使用此授权标头.
let CEP = '';
this.$http.get('https://viacep.com.br/ws/' + CEP + '/json')
.then(response => {
console.log(response.headers);
});
Run Code Online (Sandbox Code Playgroud)
这样,在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);
});
Run Code Online (Sandbox Code Playgroud)
在vue-resource文档中,可以插入对象以强制请求配置,但文档不完整.
this.$http.get('https://viacep.com.br/ws/' + CEP + '/json', {
...here...
}).then(response => {
console.log(response.headers);
});
Run Code Online (Sandbox Code Playgroud)
有没有办法删除授权字段或给定请求中的任何其他字段?
谢谢.
*更新*
通过使用拦截器(如下面的示例),我可以编辑请求,但我无法删除特定字段.
Vue.http.interceptors.push((request, next) => {
const viacep = request.url.includes('viacep.com.br');
if (viacep) {
request.headers.set('AUTHORIZATION', 'TRY THIS');
}
next(response => {});
});
Run Code Online (Sandbox Code Playgroud)
尝试删除:
Vue.http.interceptors.push((request, next) => {
const viacep = request.url.includes('viacep.com.br');
if (viacep) {
request.headers.delete('AUTHORIZATION');
}
next(response => {});
});
Run Code Online (Sandbox Code Playgroud)
Ber*_*ert 10
使用拦截器,检查请求,并在需要时删除标头.
Vue.http.interceptors.push(function(request, next) {
const removeAuthHeaders = request.url.includes("viacep.com.br");
if (removeAuthHeaders){
request.headers.delete('Access-Control-Allow-Headers')
request.headers.delete('AUTHORIZATION')
}
else {
request.headers.set('Access-Control-Allow-Headers', <value>)
request.headers.set('AUTHORIZATION', <value>)
}
next();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2651 次 |
| 最近记录: |