問題描述
所以我得到了這個 jQuery AJAX 調用,并且響應以 302 重定向的形式來自服務器.我想采用此重定向并將其加載到 iframe 中,但是當我嘗試使用 javascript 警報查看標頭信息時,它顯示為 null,即使 firebug 正確地看到它.
So I've got this jQuery AJAX call, and the response comes from the server in the form of a 302 redirect. I'd like to take this redirect and load it in an iframe, but when I try to view the header info with a javascript alert, it comes up null, even though firebug sees it correctly.
這是代碼,如果有幫助的話:
Here's the code, if it'll help:
$j.ajax({
type: 'POST',
url:'url.do',
data: formData,
complete: function(resp){
alert(resp.getAllResponseHeaders());
}
});
我真的無法訪問服務器端的東西來將 URL 移動到響應正文,我知道這將是最簡單的解決方案,因此任何有關解析標頭的幫助都會很棒.
I don't really have access to the server-side stuff in order to move the URL to the response body, which I know would be the easiest solution, so any help with the parsing of the header would be fantastic.
推薦答案
如果您使用的是舊版本的 jquery,cballou 的解決方案將可以工作.在較新的版本中,您也可以嘗試:
cballou's solution will work if you are using an old version of jquery. In newer versions you can also try:
$.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'));
}
});
根據文檔 XMLHttpRequest 對象從 jQuery 1.4 開始可用.
According to docs the XMLHttpRequest object is available as of jQuery 1.4.
這篇關于jQuery 和 AJAX 響應標頭的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!