第一部分的描述是在Unsafe JavaScript attempt to access frame, when try to upload file with ajax的问题是,我得到访问拒绝错误,当尝试上传文件与ajax的方式。
经过长时间的调试,我发现如果我不加载ape客户端,一切都可以正常工作。Ape是comet服务器http://www.ape-project.org/
Ape创建一个带有src = "http://8.ape.readbox.cz:6969/?...“的iframe。如果我禁用了ape,并且没有创建这个iframe,那么我可以访问为ajax-upload创建的iframe的文档。
它们看起来像是
<iframe src="http://8.ape.readbox.cz:6969/?..." style="display: none; position: absolute; left: -300px; top: -300px;" id="ape_undefined"></iframe>
<iframe style="display: none;" id="ValumsAjaxUpload0" src="javascript:false;" name="ValumsAjaxUpload0"></iframe>有谁能帮帮我吗?我很困惑。
发布于 2010-11-04 05:19:02
不幸的是,我不能给你一个具体的解决方案。问题的要点如下:
为了让APE进行跨域(实际上是子域) POST调用,ape需要重置document.domain (一个全局窗口变量),去掉域名前缀(www.beta等)。虽然0.ape.YOURDOMAIN.com是来自www.YOURDOMAIN.com的跨域限制,但它允许来自YOURDOMAIN.com的相同域权限。不幸的是,为了让你的上传iframe访问父窗口,它也需要来自相同的域。重置document.domain改变了这一点。
一种解决方案是将APE的默认XHR方法从'POST‘更改为'GET’。显然,您可以从外部域发出get请求,但您可以发送的数据量是有限的。此外,您必须注释掉重置页面上的document.domain的javascript行。
这可能不是您正在寻找的伟大的、可靠的解决方案,但我希望它能揭示问题的根源。
发布于 2012-04-15 02:51:23
我使用jQuery Form Plugin和APE comet服务器,使用iframe上传文件时遇到了同样的问题。以下是我的解决方案(使用静态iframe而不是动态iframe):
将以下代码添加到页面,这些代码是在启动APE之前加载的:
<iframe id="file_upload_iframe" name="file_upload_iframe" src="iframe_src.html" style="position: absolute;left: -300px;top:-300px; width:0px;height:0px;"></iframe>将页面iframe_src.html (静态iframe的初始源)添加到站点:
<html>
<head>
</head>
<body>
<script type="text/javascript">
document.domain = document.domain; <!-- this is the main line -->
</script>
</body>
</html>此页面的目的是允许表单插件进行初始表单提交,避免权限错误。
应用程序应以以下方式返回响应:
<html>
<head></head>
<body>
<script type="text/javascript">document.domain = document.domain;</script>
<textarea>' + YOUR_DATA_TO_RETURN + '</textarea>
</body>
</html>这是为了避免在第二次和以后的表单提交时出现权限被拒绝的错误。
客户端提交表单的代码:
$('#image_add_commit').click(function(){
$('#file_upload_iframe').unbind();//may be it is unnecessary
$('#image_add_form').ajaxSubmit(
{success: image_add_complete,
iframe: true,
iframeTarget: $('#file_upload_iframe').get(0),
dataType: 'html',
textarea: true});
});
function image_add_complete(data){
//data variable contains YOUR_DATA_TO_RETURN that was wrapped in HTML code
}jquery.form.js文件中的修改:
查找块
if (!s.iframeTarget) {
// add iframe to doc and submit the form
$io.appendTo('body');
if (io.attachEvent)
io.attachEvent('onload', cb);
else
io.addEventListener('load', cb, false);
}并将其更改为
if (!s.iframeTarget) {
// add iframe to doc and submit the form
$io.appendTo('body');
}
if (io.attachEvent)
io.attachEvent('onload', cb);
else
io.addEventListener('load', cb, false);(这只是将第二个大括号向上移动)
https://stackoverflow.com/questions/3012636
复制相似问题