vue如何强行停止ajax请求,VueJs和VueResource,从Ajax请求中删除头字段

当我实例化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上:

jZtQo.png

我尝试使用以下代码删除一些标题字段,但没有成功。

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);

});

有没有办法删除授权字段或给定请求中的任何其他字段?

感谢。

*更新*

通过使用拦截器(如下面的示例所示)我可以编辑请求,但我无法删除特定字段。

Vue.http.interceptors.push((request, next) => {

const viacep = request.url.includes('viacep.com.br');

if (viacep) {

request.headers.set('AUTHORIZATION', 'TRY THIS');

}

next(response => {});

});

1Me9R.png

尝试删除:

Vue.http.interceptors.push((request, next) => {

const viacep = request.url.includes('viacep.com.br');

if (viacep) {

request.headers.delete('AUTHORIZATION');

}

next(response => {});

});

NONiM.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值