How to get Response Header location from jQuery Get?

前端 未结 3 1169
猫巷女王i
猫巷女王i 2020-12-31 05:21

So I am trying to get the location from a header response via jQuery get. I tried using getResponseHeader(\'Location\') and getAllResponseHeaders() but they both seem to ret

3条回答
  •  灰色年华
    2020-12-31 05:46

    for some headers in jQuery Ajax you need to access XMLHttpRequest object

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

    or using plain javascript

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

提交回复
热议问题