【发布时间】:2015-04-26 05:56:28
【问题描述】:
我正在尝试设置一个带有输入类型文件的 html 表单。我想通过发布请求将此文件上传到侦听同一主机但端口不同的服务器。我的表单目标是一个 iframe,它在加载时接收带有新上传文件的 _id 的数据。问题是我收到“Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "http://192.168.0.105:3001" from access a frame with origin "http://192.168.0.105:3011". 协议,域,并且端口必须匹配。”下面提供了一些不必要的硬编码的示例代码
'<form id="{id}_form" action="http://192.168.0.105:3011/private/profile_picture/upload" enctype="multipart/form-data" method="post" target="{id}_uploadframe">',
'<span id="{id}_wrapper" class="file-wrapper">',
'<input id="{id}_real" type="file" accept="image/*" name="photo" />',
'<span class="button">{0}</span>',
'</span>',
'</form>',
'<iframe id="{id}_uploadframe" name="{id}_uploadframe" class="mc-hidden"></iframe>'
Ext.fly( this.id + '_uploadframe' ).on( 'load', function( evt, el )
{
var data = el.contentDocument.body.innerHTML;
try
{
data = Ext.JSON.decode( data, true );
}
catch( ex )
{
data = {};
}
........
if( data && data.success === true )
{
if (me.cbUpload) {
me.cbUpload(data);
}
.......
}, this );
}
Ext.fly( this.id + '_real' ).on( 'change', function( evt, el )
{
....
var form = document.getElementById( me.id + '_form' );
form.submit();
....
});
我知道我违反了跨域政策,但是有什么简单的方法可以绕过或破解它吗?
【问题讨论】:
标签: javascript html iframe