Javascript 如何从 jQuery Get 获取响应头位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11223946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Response Header location from jQuery Get?
提问by raeq
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);
});
回答by Bergi
The headers will be available when the asynchronousrequest 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'));
}
});
回答by uingtea
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();
回答by Glen van Ginkel
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
如您所见,无法获取响应 URL,因为 jqXHR API 没有公开它

