首页 > 解决方案 > xmlhttprequest 并将内容放入 iframe

问题描述

我有 2 个网站都在我的控制之下,并且在同一台服务器上。a.com 在 iframe 中显示 b.com,我正在尝试实现 http 标头身份验证。现在我正在做的是制作一个带有从 a.com 到 b.com 的所需标头的 xmlhttprequest,并将响应放入这样的 iframe

document.getElementById("frame").contentDocument.write(h.responseText);

当我这样做时,响应中的 window.location.href 值显示 a.com 而不是 b.com,我猜这是因为当 contentDocument.write 写入响应时,它被写入 a.com 而不是正确的iframe 容器...谁能告诉我我做错了什么以及如何解决它...

编辑:这是完整的代码,以防有人需要

var h=new XMLHttpRequest();
h.onreadystatechange=function(){
    if(h.readyState==4&&h.status==200){
        var e=document.createElement('iframe');
        e.id="frame";
        e.src="about:blank";    
        document.body.appendChild(e);
        var doc=document.getElementById("frame").contentDocument || document.getElementById("frame").contentWindow.document;                        
        doc.write(h.responseText);
    }
}
h.open('POST',"request_url_here",true);h.setRequestHeader('custom_header','custom_data');h.send();

标签: javascriptiframexmlhttprequest

解决方案


推荐阅读