prosource

jQuery AJAX GET 호출에서 요청 헤더 전달

probook 2023. 2. 22. 22:19
반응형

jQuery AJAX GET 호출에서 요청 헤더 전달

jQuery를 사용하여 AJAX GET에서 요청 헤더를 전달하려고 합니다.다음 블록에서 "data"는 쿼리 문자열의 값을 자동으로 전달합니다.그 데이터를 요청 헤더에 대신 전달할 수 있는 방법이 있습니까?

$.ajax({
         url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
         data: { signature: authHeader },
         type: "GET",
         success: function() { alert('Success!' + authHeader); }
      });

다음도 작동하지 않았습니다.

$.ajax({
         url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
         beforeSend: { signature: authHeader },
         async: false,                    
         type: "GET",
                    success: function() { alert('Success!' + authHeader); }
      });

jQuery 1.5에서는headers다음과 같이 전달할 수 있습니다.

$.ajax({
    url: "/test",
    headers: {"X-Test-Header": "test-value"}
});

http://api.jquery.com/jQuery.ajax 에서 :

headers (1.5 추가): 요구와 함께 송신하는 추가 헤더 키/ 쌍의 맵.이 설정은 beforeSend 함수를 호출하기 전에 설정됩니다.따라서 헤더 설정의 값은 beforeSend 함수 내에서 덮어쓸 수 있습니다.

사용하다beforeSend:

$.ajax({
         url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
         data: { signature: authHeader },
         type: "GET",
         beforeSend: function(xhr){xhr.setRequestHeader('X-Test-Header', 'test-value');},
         success: function() { alert('Success!' + authHeader); }
      });

http://api.jquery.com/jQuery.ajax/

http://www.w3.org/TR/XMLHttpRequest/ #set-requestheader-displays

$.ajax({
            url: URL,
            type: 'GET',
            dataType: 'json',
            headers: {
                'header1': 'value1',
                'header2': 'value2'
            },
            contentType: 'application/json; charset=utf-8',
            success: function (result) {
               // CallBack(result);
            },
            error: function (error) {
                
            }
        });

언급URL : https://stackoverflow.com/questions/3258645/pass-request-headers-in-a-jquery-ajax-get-call

반응형