如何从jQuery获取响应头位置?

时间:2021-09-23 11:03:32

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 return null.

因此,我试图通过jQuery get从标题响应中获取位置。我尝试使用getResponseHeader(“Location”)和getAllResponseHeaders(),但它们似乎都返回null。

Here's my current code

这是我当前的代码

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

3 个解决方案

#1


27  

The headers will be available when the asynchronous request returns, so you will need to read them in the success callback:

当异步请求返回时,报头将可用,因此您需要在成功回调中读取它们:

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

#2


3  

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

对于jQuery Ajax中的某些头部,您需要访问XMLHttpRequest对象。

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

或者使用纯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();

#3


0  

jQuery abstracts the XMLHttpRequest object in a so-called "super set" that does not expose the responseURL field. It's in their docs where they talk about the "jQuery XMLHttpRequest (jqXHR) object"

jQuery将XMLHttpRequest对象抽象为所谓的“超集”,不公开responseURL字段。在他们的文档中,他们讨论了"jQuery XMLHttpRequest (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.

As you can see there is no way to get hold of the response URL because the jqXHR API does not expose it

您可以看到,由于jqXHR API不公开它,所以无法获得响应URL。

#1


27  

The headers will be available when the asynchronous request returns, so you will need to read them in the success callback:

当异步请求返回时,报头将可用,因此您需要在成功回调中读取它们:

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

#2


3  

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

对于jQuery Ajax中的某些头部,您需要访问XMLHttpRequest对象。

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

或者使用纯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();

#3


0  

jQuery abstracts the XMLHttpRequest object in a so-called "super set" that does not expose the responseURL field. It's in their docs where they talk about the "jQuery XMLHttpRequest (jqXHR) object"

jQuery将XMLHttpRequest对象抽象为所谓的“超集”,不公开responseURL字段。在他们的文档中,他们讨论了"jQuery XMLHttpRequest (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.

As you can see there is no way to get hold of the response URL because the jqXHR API does not expose it

您可以看到,由于jqXHR API不公开它,所以无法获得响应URL。