form文件表单跨域提交时,无法获取远程服务器的返回结果,form提交代码如下:

form.submit({
url:'http://{remoteUrl}/hgisserver/wrds/file',
waitMsg: 'Reading your file...',
method : 'POST',
success: function(fp, o) {
     console.log(o);
},
failure: function(form, action) {
console.log(action);
}
});

报错如下:

"{success:false,message:"Blocked a frame with origin "http://localhost:8080" from accessing a cross-origin frame."}"

为解决这个问题,中间比较坎坷,http://blog.csdn.net/caostorm/article/details/74392158该博客的解决办法,我在extjs-4.2.1版本下试了没有效果,Content-Type仍然为text/plain.

现总结得到如下两种解决方案:

远程服务器已经设置了跨域请求,文件已经上传到远程服务器了,只是由于form新开的iframe无法接收服务器的返回结果。解决办法如下:

1、通过PHP代理转发请求和返回结果;

2、采用jquery的ajax请求;

第一种方法比较复杂,需设置php环境,避免文件过大无法上传,若直接将临时文件转发,文件名的传入还需在后台解析时注意;

form提交时代吗修改为:

form.submit({
url: '/php/formCrosUpload.php',
params:{
remoteUrl: "http://{remoteUrl}/hgisserver/wrds/file"
}
waitMsg: 'Reading your file...',
method : 'POST',
success: function(fp, o) {
console.log(action);
},
failure: function(form, action) {
console.log(action);
}
});

php处理:

<?php
header("Access-Control-Allow-Origin:*");
header("Access-Control-Allow-Methods:POST"); function curlPost($url,$postData)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
curl_close($ch);
return $output;
} $url = trim($_POST['remoteUrl']);
$file = $_FILES['form-file'];
$tmpFile = $file["tmp_name"]; if($file["error"] > 0){
echo "file upload error:".$file["error"];
}
else{
$post_data = array (
$file['name'] => '@'. $tmpFile  //将文件名通过Field的key传入,后台解析时需注意,$tmpFile是临时文件,文件名为null
);
echo curlPost($url,$post_data);
} ?>

在工程配置web.xml文件制定的php.ini配置文件中设置设置php的文件上传大小限制:

web.xml:
......
<init-param>
<param-name>ini-file</param-name>
<param-value>WEB-INF/php.ini</param-value>
</init-param>
...... php.ini: upload_max_filesize = 50M //最大上传文件大小
post_max_size = 50M    //最大POST大小
memory_limit = 128M     //内存限制
max_execution_time = 30  //最大执行时间
max_input_time = 60    //最大输入时间

第二种方法比较简单,可增加等待提示框,实现与form提交等待同样的效果;通过FormData装载form文件,然后POST到远程服务器

代码如下:

    var fileEl = Ext.getCmp('form-file').fileInputEl.dom;
var fd = new FormData();
fd.append("file", fileEl.files[0]); var msg = new Ext.window.MessageBox();
msg.wait('Upload your file...');
$.ajax({
url: 'http://{remoteUrl}/hgisserver/wrds/file',
type: 'POST',
cache: false,
data: fd,
processData: false,
contentType: false,
dataType:"json",
beforeSend: function(){
uploading = true;
},
success : function(data) {
console.log(data);
form.reset();
},
error: function(data) {
Ext.Msg.alert('Fail', 'Upload File failed.');
},
complete: function(){
msg.close();
}
});

最新文章

  1. centos查看系统cpu个数、核心书、线程数
  2. 如何在其他电脑上运行VS2005编译的DEBUG版应用程序
  3. EXCEL 数字+E+数字 自动识别为指数形式的解决办法
  4. Oracle Grid Infrastructure: Understanding Split-Brain Node Eviction (文档 ID 1546004.1)
  5. HTTP状态码及其含义 503 500 401 200 301 302
  6. Visual Studio 创建和使用dll的方法
  7. JAVA_SE基础——63.String类的常用方法
  8. 五十一、进程间通信——System V IPC 之进程信号量
  9. PostgreSQL学习----命令或问题小结
  10. JSP Cookies 处理
  11. xss实现钓鱼操作
  12. REST式的web服务
  13. xmapp+netbeans调试
  14. Python数据类型(数字)
  15. Windows加密API的功能分类
  16. vue-scroller的使用 &amp;&amp; 开发自己的 scroll 插件
  17. vsftpd 配置:chroot_local_user与chroot_list_enable userlist_enable userlist_deny详解
  18. 4、html的body内标签之input系列
  19. 2 Angular 2 的核心概念
  20. zoj 2676 dinic模板求实型最小割(可做dinic模板)

热门文章

  1. QT in VS 多语言实现(中英文切换,每个步骤都有截图,只有UTF8才能让Qt语言家正确读取。先qApp-&gt;removeTranslator,然后installTranslator,每个类都要写上槽函数RetranslateUI)
  2. 兼容ie的半透明背景颜色过滤器,会影响事件的触发.
  3. Python中的高级数据结构(转)
  4. mysql 约束条件 auto_increment 自动增长
  5. math.h函数库
  6. PAT 1146 Topological Order[难]
  7. [golang note] 内建类型
  8. STL: fill,fill_n,generate,generate_n
  9. 自动填写IE的网页的输入框的内容
  10. VirtualBox AndroidX86 网络设置