javascript - 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();
解决方案
推荐阅读
- dart - 如何使用 android_intent 通过颤振打开位置设置
- spring-mvc - Spring Boot @RequestParameter 意外行为
- angular - 锚点滚动仅适用于 Angular 6 中的第二次点击
- php - PHP preg_split 将分隔符保留在不同的元素中
- qt - 是否可以在 QAxObject 中链接嵌套命令?
- spring-batch - 如何使用 java config 获取 MultiResourceItemReader 的当前资源
- java - 解析后对 Groovy 扁平化的 JsonSluper 对象进行排序
- reactjs - 使用 Expo 时如何在 IOS 和 Android 上使用谷歌地图
- firebase - 具有多重身份验证的 Firebase 身份验证
- amazon-web-services - 使用 sigv4 进行身份验证的 API 网关给出“消息:缺少身份验证令牌”