Skip to main content
 首页 » 编程设计

jquery之在 jQuery 中设置 "Accept" header 以检索 iframe 中的数据

2026年10月29日5over140

我有一个网页,我要向其中附加 iframe,如下所示:

$('<iframe id="testIframe" />').src('http://www.google.nl/').appendTo('body'); 

用于加载此 iframe 内容而发送的 Accept header 设置为:

application/xml, application/xml+xhtml, text/html, text/plain, */* 

这里的问题是,响应现在以 XML 形式返回,因为接受 header 是在服务器端读取的,并且响应以接受 header 发送的格式发回(在本例中使用 application/xml)。

现在我知道我可以使用 $.load() 功能并为此设置接受 header ,但我不知道如何在 iframe 中显示返回的 HTML。例如,这显示了一个空白的 iframe:

$('#testIframe').load('http://www.google.com/', function(response) { 
    $('#testIframe').append(response); 
}); 

任何帮助将不胜感激!

请您参考如下方法:

类似这样的吗?

$('#testIframe').load('http://www.google.com/', function(response) { 
    var doc = $('#testIframe')[0].contentDocument || $('#testIframe')[0].contentWindow.document 
    doc.write(response); 
    doc.close() 
});