javascript 如何获取 iframe 响应标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17250103/
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 iframe response headers?
提问by
Is there a way to get response headers of an iframe onload?
有没有办法获取 iframe onload 的响应标头?
I have already googled it, but really I could not find something useful about it!
我已经用谷歌搜索过了,但我真的找不到有用的东西!
回答by Tim
Not really. If the iframe is on the same domain you can access its document object which contains some useful information such as document.referrer
, but you cannot intercept the full HTTP headers without making an Ajax request for the URL. This would mean requesting the URL again. e.g:
并不真地。如果 iframe 在同一个域中,您可以访问它的文档对象,其中包含一些有用的信息,例如document.referrer
,但是如果不对 URL 发出 Ajax 请求,就无法拦截完整的 HTTP 标头。这意味着再次请求 URL。例如:
$.ajax( { url: $(#myFrame).attr('src'), success: function(r,x){
console.log( x.getResponseHeader('SomeHeader') );
} } );
This will only work if the iframe src is in the same domain as the calling script.
这仅在 iframe src 与调用脚本在同一个域中时才有效。