日期:2014-05-16  浏览次数:20840 次

ExtJS4.0 文件上传的返回JSON字符串不对 - Web 开发 / Ajax
最近使用ExtJS4.0做项目,在ExtJS上传文件时发现上传成功后,ExtJS对返回的JSON字符处理报异常,而字符串的被加入了<pre></pre>,可是以为服务端返回问题,原来使用Spring的上传功能,后改用Servlet上传,ExtJS接收也是会有这样的问题,下面是ExtJS的页面代码:
JScript code
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的基本上传,各位给看看,是不是我配置还少了什么东西啊

------解决方案--------------------
回调增加下面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.