programing

jQuery.jaxSetup에 설정된 특정 요청 헤더를 제거합니다.

codeshow 2023. 9. 14. 23:43
반응형

jQuery.jaxSetup에 설정된 특정 요청 헤더를 제거합니다.

다음을 사용하여 사용자 지정 헤더를 설정했습니다.

$.ajaxSetup({
    headers : {
        'x-custom' : 'value'
    }
});

추가됩니다.x-custom모든 ajax 요청에 대한 header입니다.하지만 나는 이 헤더를 포함하지 않는 몇가지 구체적인 요청을 원합니다.

나는 이것을 시도했습니다, 그 ajaxSettings에서 그 ajaxSettings를 호출하기 전에 헤더를 삭제하고 그것이 완료되면 그것을 다시 추가합니다.

delete $.ajaxSettings.headers["x-custom"];

$.ajax({
    ...
    "success": function (data) {
        $.ajaxSettings.headers["x-custom"] = 'value';
        ...
    }
});

하지만 저는 이 방법이 올바른 방법이 아니라고 생각합니다. 통화가 끝나기 전에 실행된 요청은 해당 헤더를 얻을 수 없기 때문입니다.제가 또 무엇을 제안해 드릴 수 있을까요?

다음 행에 머리글을 다시 추가해야 합니까?$.ajax콜백으로 하는 대신에?

이 질문에는 수락으로 표시할 수 있는 답변이 없기 때문입니다.해결책을 올립니다.

AJAX 호출 직후에 헤더를 다시 추가하는 것이 타당할 것 같습니다.이렇게 하면 성공적인 콜백을 기다렸다가 콜백을 추가하지 않을 수 있습니다.

delete $.ajaxSettings.headers["x-custom"]; // Remove header before call

$.ajax({
    ...
    "success": function (data) {
        ...
    }
});

$.ajaxSettings.headers["x-custom"] = 'value'; // Add it back immediately

ajaxComplete 함수를 추가할 수 있습니다.모든 아약스 요청 후 실행되며 원하는 대로 실행됩니다.
이런 거, 이런 거.

$(document).ajaxComplete(function(event, xhr, settings) {
        // Add the headers again.
        $.ajaxSetup({
            headers : {
                "x-custom" : "value"
            }
        });
    }
});  

설명서는 여기에서 찾을 수 있습니다.
또한 jQuery 1.8부터는 .ajaxComplete() 메서드를 문서에만 첨부해야 합니다.

언급URL : https://stackoverflow.com/questions/23383891/remove-specific-request-headers-set-in-jquery-ajaxsetup

반응형