首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从jQuery Get获取响应头位置?

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

Stack Overflow用户
提问于 2012-06-27 18:17:31
回答 2查看 72.5K关注 0票数 19

因此,我尝试通过jQuery get从头响应中获取位置。我尝试使用getResponseHeader('Location')和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);
});
EN

回答 2

Stack Overflow用户

发布于 2017-01-30 13:42:01

对于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();
票数 7
EN

Stack Overflow用户

发布于 2017-08-17 21:03:41

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.

正如您所看到的,没有办法获得响应URL,因为jqXHR应用编程接口不公开它

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11223946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档