programing

jQuery Get에서 응답 헤더 위치를 가져오는 방법?

iphone6s 2023. 10. 19. 22:07
반응형

jQuery Get에서 응답 헤더 위치를 가져오는 방법?

그래서 저는 jQueryget을 통해 헤더 응답에서 위치를 알아내려고 합니다.getResponseHeader('Location')와 getAllResponseHeader()를 사용해 보았지만 둘 다 null을 반환하는 것 같습니다.

여기 제 현재 코드가 있습니다.

$(document).ready(function(){
   var geturl;
   geturl = $.ajax({
      type: "GET",
      url: 'http://searchlight.cluen.com/E5/Login.aspx?URLKey=uzr7ncj8)',
   });
   var locationResponse = geturl.getResponseHeader('Location');
   console.log(locationResponse);
});

머리글은 비동기 요청이 반환되면 사용할 수 있으므로 성공 콜백에서 머리글을 읽어야 합니다.

$.ajax({
    type: "GET",
    url: 'http://searchlight.cluen.com/E5/Login.aspx?URLKey=uzr7ncj8)',
    success: function(data, status, xhr) {
        console.log(xhr.getResponseHeader('Location'));
    }
});

jQuery Ajax의 일부 헤더의 경우 XMLHtpRequest 개체에 액세스해야 합니다.

var xhr;
var _orgAjax = jQuery.ajaxSettings.xhr;
jQuery.ajaxSettings.xhr = function () {
  xhr = _orgAjax();
  return xhr;
};

$.ajax({
    type: "GET",
    url: 'http://example.com/redirect',
    success: function(data) {
        console.log(xhr.responseURL);
    }
});

또는 일반 자바스크립트를 사용합니다.

var xhr = new XMLHttpRequest();
xhr.open('GET', "http://example.com/redirect", true);

xhr.onreadystatechange = function () {
  if (this.readyState == 4 && this.status == 200) {
    console.log(xhr.responseURL);
  }
};

xhr.send();

jQuery는 응답을 노출하지 않는 소위 "슈퍼 세트"에서 XMLHttpRequest 개체를 추상화합니다.URL 필드.문서에서 "jQuery XMLHtpRequest(jqXHR) 개체"에 대해 이야기합니다.

For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods:

readyState
responseXML and/or responseText when the underlying request responded with xml and/or text, respectively
status
statusText
abort( [ statusText ] )
getAllResponseHeaders() as a string
getResponseHeader( name )
overrideMimeType( mimeType )
setRequestHeader( name, value ) which departs from the standard by replacing the old value with the new one rather than concatenating the new value to the old one
statusCode( callbacksByStatusCode )
No onreadystatechange mechanism is provided, however, since done, fail, always, and statusCode cover all conceivable requirements.

보시다시피 응답 URL은 jqXHR API에서 노출되지 않기 때문에 파악할 방법이 없습니다.

언급URL : https://stackoverflow.com/questions/11223946/how-to-get-response-header-location-from-jquery-get

반응형