首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

ExtJS4.0 文件下传的返回JSON字符串不对

2012-12-25 
ExtJS4.0 文件上传的返回JSON字符串不对本帖最后由 showbo 于 2011-12-02 11:22:03 编辑最近使用ExtJS4.0

ExtJS4.0 文件上传的返回JSON字符串不对
本帖最后由 showbo 于 2011-12-02 11:22:03 编辑 最近使用ExtJS4.0做项目,在ExtJS上传文件时发现上传成功后,ExtJS对返回的JSON字符处理报异常,而字符串的被加入了<pre></pre>,可是以为服务端返回问题,原来使用Spring的上传功能,后改用Servlet上传,ExtJS接收也是会有这样的问题,下面是ExtJS的页面代码:

var form =Ext.create('Ext.form.Panel', {
id:'image_up',
frame : true,
buttonAlign : 'center',
fileUpload: true,  //上传文件
scope : this,
 renderTo:Ext.getBody(),
items : [
{
fieldLabel : '上传图片',
labelAlign:'right',
name : 'imgFile',
id : 'imgFile',
xtype : 'filefield',
labelWidth : 60,
archor:'100%',
width:350,
height:25,
buttonText:'选择图片……',
colspan:2
}],
buttons : [ {
text : '提交',
handler : function() {
form.form.submit({
clientValidation : true,
url : 'TestUpload',
method : 'POST',
waitMsg:'正在上传图片',
success : function(form, action) {
var data = Ext.JSON.decode(action.response.responseText).data;
Ext.Msg.alert('提示', '添加成功');
window.close();
},
failure : function() {
var data = Ext.JSON.decode(action.response.responseText);
Ext.Msg.alert('提示', data.errors);
}
});
}
}, {
text : '重置',
handler : function() {
reset(form);
}
} ]
});
});

后台上传不贴了,就是Java的基本上传,各位给看看,是不是我配置还少了什么东西啊
[最优解释]
本帖最后由 showbo 于 2011-12-02 11:35:06 编辑 回调增加下面2句试试,ext不会更改服务器返回值的,只可能是你服务器没配置对什么的,导致返回pre标签


不知道java怎么弄了。。

success : function(form, action) {
alert(action.response.responseText)///////
                                    var data = Ext.JSON.decode(action.response.responseText).data;
                                    Ext.Msg.alert('提示', '添加成功');
                                    window.close();
                                },
                                failure : function() {
alert(action.response.responseText)///////
                                    var data = Ext.JSON.decode(action.response.responseText);
                                    Ext.Msg.alert('提示', data.errors);


                                }

[其他解释]
var targetForm = this.down("*[name=uploadForm]").getForm();
Ext.Msg.show({
title:'',
msg: '',
buttons: Ext.Msg.YESNO,
icon: Ext.Msg.QUESTION,
defaultFocus: 'YES',
fn:function(btn){
if(btn == 'yes') {
        targetForm.submit({
        url: 'upload',
success: function(response){
Ext.MessageBox.alert("上传成功");

},
failure: function(response)
{
return;
}

});
}
else
return;
}
});

这是以前做的一个项目的上传功能,后台是不返回值的。好像是有返回值的话,会弹出下载的对话框。
[其他解释]
success: function(response){
var data = Ext.decode(response.responseText);
[其他解释]
这个就晓不得了。。感觉ext没这么傻吧。。自动增加pre标签。
[其他解释]
uncaught exception: You're trying to decode an invalid JSON String: <pre>{success:true}</pre>
这是firefox调试报的异常,success和failure代码就没执行到,我新建了简单项目测试也是这问题
[其他解释]
肯定不是服务端的问题,通过firefox可以看到服务端的返回是正确的,看来只是ExtJS解析的问题了
[其他解释]
问题解决了,我修改了返回类型为setContentType("text/html")可以正确解析了,感到很奇怪,其他的地方使用
setContentType("application/Json")都没问题,就上传的返回有问题了。
success:function(){}这里的解析是extJS解析收到的返回json后才会执行的,如果你的json字符串有问题,这里是不会执行的,好了结贴了。
[其他解释]

引用:
回调增加下面2句试试,ext不会更改服务器返回值的,只可能是你服务器没配置对什么的,导致返回pre标签


不知道java怎么弄了。。

success : function(form, action) {
alert(action.response.responseText)///////
                                    var data =……


哥,你咋那么S*B呢?

热点排行