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上:
我嘗試使用以下代碼刪除一些標題字段,但沒有成功。
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 => {});
});
嘗試刪除:
Vue.http.interceptors.push((request, next) => {
const viacep = request.url.includes('viacep.com.br');
if (viacep) {
request.headers.delete('AUTHORIZATION');
}
next(response => {});
});
總結
以上是生活随笔為你收集整理的vue如何强行停止ajax请求,VueJs和VueResource,从Ajax请求中删除头字段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 数组转音频_Python3
- 下一篇: 客制化键盘编程_客制化键盘如何入坑?