【发布时间】:2013-11-22 18:55:59
【问题描述】:
我在另一个域的 iFrame 中有一个 uploadify 脚本。我正在尝试将文件上传data 发送到嵌入 iFrame 的页面。
我在 iFrame(上传脚本)中有以下代码:
$('#file_upload').uploadify({
//JS CODE
'onUploadSuccess' : function(file, data, response) {
data //data is what must be passed from the iFrame to the script on another site
}
});
data 是必须传递给另一个域上的以下脚本的内容:
var myframe, nestedFrame;
myFrame = $('#editorf').contents().find('body');
nestedFrame = myFrame.find('#re_contentIframe').contents().find('body');
nestedFrame.append('data'); //data should be replaced with the information from the iFrame
我确实尝试了以下代码:
iFrame 代码 - 页 B
$('#file_upload').uploadify({
//JS CODE for UPLOADIFY
'onUploadSuccess' : function(file, data, response) {
window.postMessage('http://iframe-domain.net/uploads/' + data,'http://iframe-domain.net');
}
});
接收页面代码 - 页面 A
$(function() {
window.addEventListener('message', receiver, false);
function receiver(e){
if (e.origin == 'http://iframe-domain.net'){
var myframe, nestedFrame;
myFrame = $('#editorf').contents().find('body');
nestedFrame = myFrame.find('#re_contentIframe').contents().find('body');
nestedFrame.append(e.data);
}
}
});
这不起作用。我确实收到此错误消息:
Error: Permission denied to access property 'toString'
...a);if(Z){var Y=0;(function(){if(typeof Z.GetVariable!=D){var ab=Z.GetVariable("$...
jquery.uploadify.js (line 17)
uploadify 脚本在文件上传时确实有效,但它似乎没有将数据传递到页面。我不相信这个错误是页面无法正常工作的原因。
我该怎么做?
编辑
为了更好地解释这里是一个例子:
一个人转到页面 A(主页)。在页面 A 上,该页面上嵌入了 iFrame。 iFrame 内部是一个 uploadify 脚本,允许用户上传文件。
一个人上传一个文件,uploadify 脚本返回文件名。示例:528050f030522.jpg。一旦uploadify 脚本获得此信息,它应该将其发送到页面A 的脚本,然后该脚本运行并将文件名插入到页面中。
【问题讨论】:
-
您能否举例说明您通过 postmessage 传递的数据?
-
简单:只需使用 top.postMessage 而不是 window.postMessage
-
@KristofDegrave - 阅读我的编辑。我提供了一个例子。
-
@dandavis - 没用。 =
-
我重新阅读了您的设置,因为最初我认为涉及 3 个域,但您似乎只想将数据从 iframe 发送回父页面。在这个 dandavis 中是正确的,您应该使用 window.top.postMessage 或 window.parent.postMessage 而不是 window.postMessage。但是,您的问题似乎在其他地方......我认为这个 sn-p 是问题
nestedFrame.append('data');。老实说,要弄清楚问题中发生的事情的描述有点困难......
标签: javascript jquery iframe cross-domain uploadify