在jQuery中设置"Accept"标头,以便在iframe中检索数据

Rev*_*ell 5 ajax iframe jquery header

我有一个网页,我附加iframe如下:

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

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

application/xml, application/xml+xhtml, text/html, text/plain, */*
Run Code Online (Sandbox Code Playgroud)

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

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

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

任何帮助将不胜感激!

Her*_*lur 0

像这样的东西吗?

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