source

VueJs 및 VueResource, Ajax 요청에서 헤더 필드 제거

factcode 2022. 8. 12. 23:14
반응형

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

이렇게 하면 접근컨트롤 요구 헤더로 인가 필드가 전송됩니다.

요청 헤더

다음 코드로 일부 헤더필드를 삭제하려고 했지만 실패했습니다.

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 => {});
});

여기에 이미지 설명 입력

인터셉터를 사용하여 요청을 검사하고 필요에 따라 헤더를 삭제합니다.

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

언급URL : https://stackoverflow.com/questions/43242730/vuejs-and-vueresource-removing-header-fields-from-a-ajax-request

반응형