Sha*_*ane 153 ajax jquery redirect header http-status-code-302
所以我有这个jQuery AJAX调用,响应来自服务器的302重定向形式.我想采用这种重定向并将其加载到iframe中,但是当我尝试使用javascript警报查看标题信息时,即使firebug正确地看到它,它也会出现空值.
这是代码,如果它有帮助:
$j.ajax({
type: 'POST',
url:'url.do',
data: formData,
complete: function(resp){
alert(resp.getAllResponseHeaders());
}
});
Run Code Online (Sandbox Code Playgroud)
我真的无法访问服务器端的东西,以便将URL移动到响应主体,我知道这将是最简单的解决方案,因此任何有关解析标头的帮助都会非常棒.
rov*_*sen 171
如果您使用旧版本的jquery,cballou的解决方案将会起作用.在较新的版本中,您还可以尝试:
$.ajax({
type: 'POST',
url:'url.do',
data: formData,
success: function(data, textStatus, request){
alert(request.getResponseHeader('some_header'));
},
error: function (request, textStatus, errorThrown) {
alert(request.getResponseHeader('some_header'));
}
});
Run Code Online (Sandbox Code Playgroud)
根据文档,XMLHttpRequest对象从jQuery 1.4开始提供.
acd*_*ior 131
如果这是一个CORS请求,您可能会看到调试工具中的所有标头(例如Chrome-> Inspect Element-> Network),但是xhr.getResponseHeader('Header')
如果这样的标头是一个简单的响应标头,xHR对象将只检索标头(via ):
Content-Type
Last-modified
Content-Language
Cache-Control
Expires
Pragma
如果它不在此集中,则它必须存在于服务器返回的Access-Control-Expose-Headers标头中.
关于这个案例,如果它是一个CORS请求,只有当下面的标题也存在时,才能Location
通过该XMLHttpRequest
对象检索标题:
Access-Control-Expose-Headers: Location
Run Code Online (Sandbox Code Playgroud)
如果它不是CORS请求,XMLHttpRequest
则检索它没有问题.
kei*_*ics 32
var geturl;
geturl = $.ajax({
type: "GET",
url: 'http://....',
success: function () {
alert("done!"+ geturl.getAllResponseHeaders());
}
});
Run Code Online (Sandbox Code Playgroud)
DRa*_*hal 18
关于AJAX和302重定向的不幸事实是你无法从返回中获取头文件,因为浏览器永远不会将它们提供给XHR.当浏览器看到302时,它会自动应用重定向.在这种情况下,你会看到firebug中的标题,因为浏览器得到它,但你不会在ajax中看到它,因为浏览器没有通过它.这就是成功和错误处理程序永远不会被调用的原因.只调用完整的处理程序.
http://www.checkupdown.com/status/E302.html
The 302 response from the Web server should always include an alternative URL to which redirection should occur. If it does, a Web browser will immediately retry the alternative URL. So you never actually see a 302 error in a Web browser
Run Code Online (Sandbox Code Playgroud)
以下是该主题的一些stackoverflow帖子.一些帖子描述了解决这个问题的黑客行为.
Ple*_*and 10
jQuery使用的基础XMLHttpRequest对象将始终默默地遵循重定向,而不是返回302状态代码.因此,您无法使用jQuery的AJAX请求功能来获取返回的URL.相反,您需要将所有数据放入表单并提交表单,并将target
属性设置为name
iframe 的属性值:
$('#myIframe').attr('name', 'myIframe');
var form = $('<form method="POST" action="url.do"></form>').attr('target', 'myIframe');
$('<input type="hidden" />').attr({name: 'search', value: 'test'}).appendTo(form);
form.appendTo(document.body);
form.submit();
Run Code Online (Sandbox Code Playgroud)
服务器的url.do
页面将加载到iframe中,但当其302状态到达时,iframe将被重定向到最终目标.
试试这个:
type: "GET",
async: false,
complete: function (XMLHttpRequest, textStatus) {
var headers = XMLHttpRequest.getAllResponseHeaders();
}
Run Code Online (Sandbox Code Playgroud)
JQUERY 3 及更高版本的 2018 年更新
我知道这是一个老问题,但上述解决方案都不适合我。这是有效的解决方案:
//I only created this function as I am making many ajax calls with different urls and appending the result to different divs
function makeAjaxCall(requestType, urlTo, resultAreaId){
var jqxhr = $.ajax({
type: requestType,
url: urlTo
});
//this section is executed when the server responds with no error
jqxhr.done(function(){
});
//this section is executed when the server responds with error
jqxhr.fail(function(){
})
//this section is always executed
jqxhr.always(function(){
console.log("getting header " + jqxhr.getResponseHeader('testHeader'));
});
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
304917 次 |
最近记录: |