将 JSON 对象发布到 iFrame

问题描述 投票:0回答:3

我见过将数据发布到 iframe 的不同方法,但我找不到可以仅发送 JSON 对象的方法。所有方法似乎都要求我使用表单元素来放入我的数据。

javascript json http post iframe
3个回答
7
投票

查看 postMessage 并在消息处理程序中使用 JSON.stringify 和 JSON.parse。

要真正发布到 iframe,你必须这样做

myIframe.contentWindow.postMessage(...)

小提琴

html

<button onclick="_sendMessage ()">Send</button>
<iframe src="" id="myIframe">​

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), '*');
}​

1
投票

您可以使用Porthole JS 库。它将自己描述为“用于安全跨域 iFrame 通信的 JavaScript 库”

如果可用,它会使用

postMessage()
,但对于不可用的浏览器,将恢复为“隐藏代理”解决方法。


0
投票

我在.NET中开发,然后,我从服务器加载json文件,从路径中获取文件并转换为Base64字符串,如下所示:

ASPX:

C# byte[] documento_json = System.IO.File.ReadAllBytes("这是你的路径");

然后: 字符串base64_json = Convert.ToBase64String(documento_json);

终于 ifJsonFile.Src = string.Format("data:application/pdf;base64,{0}", base64_json);

这对我来说是工作

© www.soinside.com 2019 - 2024. All rights reserved.