如何从jQuery的响应头位置获取?(How to get Response Header loca

2019-06-25 19:58发布

所以我想从通过jQuery得到一个标头响应得到的位置。 我尝试使用getResponseHeader(“位置”)和getAllResponseHeaders(),但他们似乎都返回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);
});

Answer 1:

该标题将可当异步请求返回,所以你需要在阅读他们的成功回调 :

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


Answer 2:

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

或使用普通的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();


Answer 3:

jQuery的抽象的所谓的“超集”不暴露responseURL场XMLHttpRequest对象。 这是在他们的文档,他们谈“的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.

正如你可以看到有没有办法得到的回应URL的保持,因为jqXHR API不公开其



文章来源: How to get Response Header location from jQuery Get?