1. 程式人生 > >extjs中submit提交後不進入success也不進入failure 解決方法

extjs中submit提交後不進入success也不進入failure 解決方法

首先說明是後臺返回的json返回值格式不對

要執行successfailure,需在返回的json中有如下欄位 :

執行success裡面的操作
{success:true}

執行failure裡面的操作
{success:false}

首先要確定你Extjs上傳檔案程式碼正確性(提供部分程式碼)

buttons : [ {
            text : '上傳檔案',
            handler : function() {
                var form = this.up('form').getForm();
                var
fileName = Ext.getCmp("fileid").getValue(); fileName = fileName.split('.'); fileName = fileName[fileName.length - 1]; if (fileName != "doc" && fileName != "docx") { Ext.Msg.alert("系統提示", "必須選擇Microsoft Office Word 文件!"); return
false; } if (form.isValid()) { form.submit({ method : 'post', url : 'announce/upFile.do', waitMsg : '檔案上傳中,請稍等...', success : function(fp, o) { Ext.Msg.alert('成功'
, '上傳成功!'); } }); } } } ]

後臺Java返回程式碼

特別提醒:response.getWriter().print("{success:true}");
返回字串的格式為:{success:true}或{success:false}

/* 附件上傳 */
    @RequestMapping("/upFile")
    public @ResponseBody void upFile(HttpServletRequest request, HttpServletResponse response,
            @RequestParam("file") CommonsMultipartFile file) throws IOException {
        /* 獲取專案工程的實際目錄,並且在該真實路徑下建立一個uploadFiles目錄 */
        String filePath = request.getSession().getServletContext().getRealPath("/uploadFiles/");
        /* 獲取檔名 */
        String fileName = file.getOriginalFilename();
        /* 獲取檔案的字尾,通過後綴可分辨檔案的型別 */
        String fileType = fileName.split("[.]")[1];
        /* 為了避免檔名重複,在檔名前加UUID */
        String uuid = UUID.randomUUID().toString().replace("-", "").substring(2, 10);
        String uuidFileName = uuid + fileName;
        /* 檔案上傳 */
        FileUtil.upFile(file.getInputStream(), uuidFileName, filePath);
        response.getWriter().print("{success:true}");
    }