Javascript 如何从jQuery get获取响应头位置?
因此,我试图通过jQuery get从报头响应中获取位置。我尝试使用getResponseHeader('Location')和getAllResponseHeaders(),但它们似乎都返回null 这是我目前的密码Javascript 如何从jQuery get获取响应头位置?,javascript,jquery,ajax,response,Javascript,Jquery,Ajax,Response,因此,我试图通过jQuery get从报头响应中获取位置。我尝试使用getResponseHeader('Location')和getAllResponseHeaders(),但它们似乎都返回null 这是我目前的密码 $(document).ready(function(){ var geturl; geturl = $.ajax({ type: "GET", url: 'http://searchlight.cluen.com/E5/Login.aspx?
$(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);
});
当异步请求返回时,标头将可用,因此您需要在以下位置读取它们:
对于jQueryAjax中的某些头,您需要访问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();
jQuery在一个不公开responseURL字段的所谓“超集”中抽象XMLHttpRequest对象。在他们的文档中,他们讨论了“jqueryxmlhttprequest(jqXHR)对象”
正如您所看到的,没有办法获得响应URL,因为JQXHRAPI没有公开它可能是重复的,可能是重复的,但没有成功尝试过。url未返回成功语句。我也尝试在false上打印出来,但没有lucktry a
complete:function(xhr){console.log(xhr.getAllResponseHeaders());}
出现的错误可能是由于相同的源策略。@raeq这有助于我:仅当响应状态为200时才调用success方法,但在这种情况下,我认为这是页面上唯一一个具有任何形式正确性的答案,jQuery既不公开responseUrl,也不提供响应头中的位置。这不是一个跨浏览器兼容的解决方案。IE不支持responseURL属性(),因此在IE中始终未定义。
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();
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.