本文介绍了将JSON对象发布到iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到了将数据发布到iframe的不同方法,但我找不到可以发送JSON对象的方法。所有方法似乎都要求我使用表单元素来放入我的数据。
I've seen different methods for posting data to an iframe but I can't find one where I can just send a JSON object. All the methods seem to require me to use form elements to put my data in.
推荐答案
看看并使用JSON.stringify为您的消息和事件处理程序中的JSON.parse 。
Take a look at postMessage and use JSON.stringify for your message and JSON.parse in the event handler.
要实际发布到iframe,你必须这样做
To actually post to a iframe you have to do
myIframe.contentWindow.postMessage(...)
html
<button onclick="_sendMessage ()">Send</button>
<iframe src="" id="myIframe">
javascript
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), '*');
}
这篇关于将JSON对象发布到iFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!