ant*_*nyv 5 javascript iframe post json http
我已经看到了将数据发布到 iframe 的不同方法,但我找不到可以只发送 JSON 对象的方法。所有的方法似乎都要求我使用表单元素来放入我的数据。
查看postMessage并在事件处理程序中将 JSON.stringify 用于您的消息和 JSON.parse。
要实际发布到 iframe,您必须执行
myIframe.contentWindow.postMessage(...)
Run Code Online (Sandbox Code Playgroud)
html
<button onclick="_sendMessage ()">Send</button>
<iframe src="" id="myIframe">?
Run Code Online (Sandbox Code Playgroud)
javascript
var myIframe = document.getElementById('myIframe');
myIframe.contentWindow.addEventListener('message', function(event) {
console.log(JSON.parse(event.data));
}, false);
window._sendMessage = function() {
var json = {payload:'Hello World'};
myIframe.contentWindow.postMessage(JSON.stringify(json), '*');
}?
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19394 次 |
| 最近记录: |