我有一个来自另一个域的页面呈现到我的主网站中,并定位为显示相关内容:
<div id="containerdivonparentsite" style="position:relative;overflow:hidden;width:350px;height:650px;">
<iframe style="position:absolute;top:-230px;left:-700px;"
scrolling="no" width="20000" height="5000"
src="http://www.otherdomain.form.com/myform" ></iframe>
</div>
呈现的子页面包含一个带有提交按钮的表单。当用户单击提交按钮时,将发出 POST HTTP 请求。是否可以在不启用 CORS 的情况下以某种方式“ Hook ”父站点中的此 POST 请求?
我想要类似的东西
$('#containerdivonparentsite').onPOST(function (request) {
alert('A POST request was issued by something inside this div. Probably the user clicked a submit button in the iframe.');
})
或者可能是全局 POST 事件订阅。
$(document).onPOST(function (request){alert('A POST request was issued on the site. It might be coming from an iframe');})
最佳答案
CORS 根本不会帮助你解决这个问题。
要跨不同来源的框架进行通信,您需要使用 postMessage
和 friend 。
与 CORS 一样,这确实需要两个站点进行合作。
父站点需要监听消息事件。表单处理程序页面需要包含一个名为 postMessage
的脚本,并包含消息中所需的任何数据。
如果没有其他站点(CORS、JSONP、postMessage 等)或浏览器(扩展)的配合,一个站点无法访问浏览器与另一个站点之间的通信。
关于javascript - 是否可以对从没有 CORS 的 iframe 发出的 POST 请求使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28110011/