用ajaxFileUpload上传文件时,文件已经发出去,远程的服务器已经收到并且给了返回值,却无论如何也跳不进success,总是跳进error
报错信息为:
Blocked a frame with origin "http://localhost:8201" from accessing a cross-origin frame.
代码如下:
$.ajaxFileUpload({
url:url,
secureuri: false,
fileElementId: fileElementId,
dataType: 'json',
data:{
destination:1,
fileName:fileName //'leave.snaker'
},
success: function(res, state) {
ajaxTips({message:1,code:1}, task_config);
res.fileName = fileName;
res.destination = 1;
callback(res);
},
error: function(XMLHttpRequest,status,e) {
console.log(e.message);
httpRequestError(XMLHttpRequest, url);
}
});
另我在子页面用的是iframe框架,哪位大神可以指点迷津,感激不尽
有没有跑过js的DeBug?
dataType: 'json'去掉 试下 。 会不会是数据格式问题?
服务器端代码返回OK?
ajaxFileUpload用iframe模拟的,你都说跨域了那肯定是无法获取iframe中内容的,会报禁止访问的错误。
如果是跨2级域名,调用ajaxfileupload的页面和接受文件的页面都输出js设置document.domain='顶级域名'来实现跨域操作
跨顶级域你只能修改源代码了,用html5的postMessage来通信
这种跨域的请求在谷歌,火狐 是没有问题的,但是在ie上是行不通的。
第一需要修改返回值:
dataType:返回iframe页面打印的值。
可以参照一下代码
function ajaxSubmitForm() {
var option = {
url: '',
type: 'POST',
dataType: 'text/html',
headers: {"ClientCallMode": "ajax"}, //添加请求头部
success: function (data) {
data = eval('(' + data + ')');
if (data.code == '0') {
window.opener.RefreshList();
window.close();
} else {
alert(data.msg);
}
},
error: function () {
alert("导入失败,请联系管理员!");
}
};
$("#upload_form").ajaxSubmit(option);
}
后台:
返回 数据类型为 contentType text/plain
补充一下 :自己拼接的json字符串,方便前台转换。
text += "{\"code\":\"1\"";
text += ",\"msg\":\"文件格式错误\"}";
Struts2Utils.renderText(text);