提交表单的响应是添加<前>到了json

时间:2013-08-09 15:18:53

标签: extjs

我正在使用ext 4.0.7。我试图上传文本文件并进行处理。这是我的js代码

}, {
    xtype: 'form',
    id: 'uploadAccountForm',
    name: 'uploadAccountForm',
    border: false,
    padding: '5px, 45px',
    //      standardSubmit: true,
    renderTo: Ext.getBody(),
    enctype: 'multipart/form-data',
    fileUpload: true,
    items: [{
            xtype: 'fileuploadfield',
            minWidth: 96,
            name: 'file',
            id: 'multipacAccountList',
            fieldLabel: bundle.getMsg('label.modal.additional_tools.multipacadmin.label.accountlist'),
            buttonText: bundle.getMsg('label.modal.additional_tools.multipacadmin.button.upload'),
            buttonOnly: false,
            vtype:'fileUpload'
        }]
},{    
    xtype: 'button',
    text: bundle.getMsg('label.modal.additional_tools.multipacadmin.button.getresults'),
    minWidth: 96,
    margin: 10,
    name: 'multipacGet',
    id: 'multipacGet',
    handler: function() {
        var account = Ext.getCmp('multipacAccount').getValue();
        var company = Ext.getCmp('multipacCompany').getValue();
        var accountList = Ext.getCmp('multipacAccountList').getValue();
        var companyList = Ext.getCmp('multipacCompanyList').getValue();
        var dlg = this.up('multiPacAdminDlg');
        if (account !== "" && company !== "") {
            Ext.MessageBox.alert('iCRM', Ext.String.format(bundle.getMsg('label.modal.additional_tools.multipacadmin.button.getresults.both')));
        } else if (account !== "") {
            iCRM.app.fireEvent("showMultiPacDefModal");
        //    getResults("account", account, dlg.down('#grid'));
        } else if (company !== "") {
            iCRM.app.fireEvent("showMultiPacDefModal");
        //    getResults("company", company, dlg.down('#grid'));
        } else if (accountList !== "" && companyList !== "") {
            Ext.MessageBox.alert('iCRM', Ext.String.format(bundle.getMsg('label.modal.additional_tools.multipacadmin.button.getresults.both')));
            var accountList = Ext.getCmp('multipacAccountList');
            accountList.superclass.setValue.call(accountList, "");
            var companyList = Ext.getCmp('multipacCompanyList');
            companyList.superclass.setValue.call(companyList, "");
        } else if (accountList !== "") {
            this.fileUpload("account");
        } else if (companyList !== "") {
            this.fileUpload("company");
        } else {
            Ext.MessageBox.alert('iCRM', Ext.String.format(bundle.getMsg('error.search.regular.missing_criteria')));
        }
    },
    fileUpload: function(category) {
        var form = "";
        var url = "";
        if (category === "company") {
            url = "../search/uploadCompanies";
            form = Ext.getCmp('uploadCompanyForm').getForm();
        } else if (category === "account") {
            url = "../search/uploadAccounts";
            form = Ext.getCmp("uploadAccountForm").getForm();
        }
        debugger;
        if (form.isValid()) {
            form.submit({
                url: url,
                method:'POST',
                waitMsg: 'Uploading your file...',
                success:function (form, action) {
                   Ext.Msg.alert('Success', 'Your file has been uploaded.');
                },
                failure: function (form, action) {
                    if (action.result.code==='Invalid Record') {
                        Ext.Msg.alert("Error", Ext.String.format(bundle.getMsg('label.modal.additional_tools.multipacadmin.upload.records.message')));
                    } else  {
                        Ext.Msg.alert("Error", "Your file has NOT been uploaded. " + action.result.code);
                    }
                },
                callback:function (form, action) {
                    debugger;
                   Ext.Msg.alert('Success', 'Your file has been uploaded. Callback');
                }
            });
        }
    }
}, {

这是我的java代码

@RequestMapping(value = "/uploadAccounts",  method = {RequestMethod.POST})
    public
    @ResponseBody
    Map<String, ? extends Object> uploadAccounts(FileUploadBean uploadItem, BindingResult result) throws IOException {

        CommonsMultipartFile accountFile = uploadItem.getFile();
        String[] records = processFile(accountFile);

        Map<String, Object> jsonMap = new HashMap<String, Object>();
        if (records != null) {
            if (records.length>100){
                jsonMap.put("success", Boolean.TRUE);
                jsonMap.put("code", "Too many Record");
                jsonMap.put("data", records);
            } else {
                jsonMap.put("success", Boolean.TRUE);
                jsonMap.put("data", records);
            }
        } else {
            jsonMap.put("success", Boolean.FALSE);
            jsonMap.put("code", "Invalid Record");
        }
        return jsonMap; 
    }

文件已上传,我可以毫无问题地处理它。但是,当我将回复发送给我的js时,我得到了这个:

Ext.Error:您正在尝试解码无效的JSON字符串:&lt; pre&gt; {&#34; data&#34;:[&#34; 876567&#34;,&#34; 676567&#34;,&#34; 678988&#34;,&#34; 654345&#34;, &#34; 654321&#34;&#34; 665654&#34;&#34; 676556&#34;&#34; 678780&#34;&#34; 909877&#34;&#34; 454343& #34;&#34; 434343&#34],&#34;成功&#34;:真}

我用firebug分析了我的回答,我可以看到答案只是: {&#34;数据&#34;:[&#34; 876567&#34;&#34; 676567&#34;&#34; 678988&#34;&#34; 654345&#34;&#34 ; 654321&#34;&#34; 665654&#34;&#34; 676556&#34;&#34; 678780&#34;&#34; 909877&#34;&#34; 454343&#34; &#34; 434343&#34],&#34;成功&#34;:真}

所以,在某个地方,某种程度上,我已经添加了&lt;前&gt;标签。我发现的唯一解决方案是修改ext核心,将这些行添加到解码函数中,以删除&lt;前&gt;或者&lt;嵌入&gt;标签。

Ext.each([/<\/?pre[^>]*>/g, /<\/?embed[^>]*>/g], function (s) {            
    json = json.replace(s, "");
});

但这不是我们正在寻找的解决方案。

我尝试了一个standardSubmit,但是这会重定向一个新的页面,标签或iframe,并且我没有从我提交的内容中找到成功/失败函数 我也尝试设置对application / json contentType或html / txt的响应......但是我得到了同样的响应。
而且,据我所知,我无法执行ajax请求,因为我正在上传文件。 那么,任何想法或建议? 提前致谢

1 个答案:

答案 0 :(得分:4)

我终于找到了解决方案:    对于Ext 4.0,您需要设置响应的contentType,如“html / text”,然后使用标签将json字符串包围。

请查看此信息以获取更多信息。 http://www.sencha.com/forum/showthread.php?269683-Response-of-a-submit-form-is-adding-lt-pre-gt-to-json&p=987969#post987969