下面是ajax代碼和Controller層代碼,期初以為是后臺程序?qū)戝e了。
$("#sourcefile").ajaxSubmit({ type: "post", dataType: "json", // 'xml', 'script', or 'json' (expected server response type) url: "/springMVC/upload/up", success: function (result) { if (result) { alert(result.col0); } }, error:function(data, XMLHttpRequest, textStatus, errorThrown){ alert(1); } });
@RequestMapping(value="/upload/up") public @ResponseBody ExcelName upload(@RequestParam("sourceFile") MultipartFile sourceFile, HttpServletRequest request, ModelMap model,HttpServletResponse response) { //判斷文件是否為空 if (sourceFile==null) return null; //獲取文件名 String name=sourceFile.getOriginalFilename(); System.out.println("name"); //進(jìn)一步判斷文件是否為空(即判斷其大小是否為0或其名稱是否為null) long size =sourceFile.getSize(); if (name==null ||("").equals(name) size==0) return null; //批量導(dǎo)入。參數(shù):文件名,文件。 ListExcelName> cpolicyList = ExcelUtils.batchImport(name,sourceFile); //迭代添加信息(注:實(shí)際上這里也可以直接將cpolicyList集合作為參數(shù),在Mybatis的相應(yīng)映射文件中使用foreach標(biāo)簽進(jìn)行批量添加。) for( ExcelName customer:cpolicyList){ colDataService.insertData(customer); } ExcelName e1=new ExcelName(); e1.setCol0("success"); return e1; }
后打點(diǎn)跟蹤后臺發(fā)現(xiàn),原來因?yàn)樯蟼靼存Itype寫成了submit導(dǎo)致提交了一次action,致使ajax未獲取到返回結(jié)果走了error。
下面是修改正確后的jsp
%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> html> head> meta http-equiv="Content-Type" content="text/html; charset=utf-8"> title>上傳/title> script type="text/javascript" src="./jquery-3.1.1.js">/script> script type="text/javascript" src="./jquery.form.js">/script> script type="text/javascript"> function submitImport(){ var epath = $('#source_file').val(); if(epath==""){ alert( '導(dǎo)入文件不能為空!'); return; } if (epath.substring(epath.lastIndexOf(".") + 1).toLowerCase()!="xlsx") { alert( '導(dǎo)入文件類型必須為excel!'); return; } $("#sourcefile").ajaxSubmit({ type: "post", dataType: "json", // 'xml', 'script', or 'json' (expected server response type) url: "/springMVC/upload/up", success: function (result) { if (result) { alert(result.col0); } }, error:function(data, XMLHttpRequest, textStatus, errorThrown){ alert(1); } }); } //partExport function downloadTemplate() { document.sourcefile.action = "/springMVC/upload/partExport"; form.submit(); //表單提交 } /script> /head> body> div> form id="sourcefile" name="sourcefile" action="" method="post" enctype="multipart/form-data"> input type="button" value="添 加" onClick="addAirLine()" /> input style="margin-left: 20px;" id="source_file" name="sourceFile" type="file" value="選擇文件" /> input style="margin-left: 20px;" data-loading-text="請勿重復(fù)提交" type="button" value="上 傳" onClick="submitImport()"> input style="margin-left: 20px;" type="submit" value="下載模板" onClick="downloadTemplate();"> /form> /div> /body> /html>
以上這篇解決ajax提交到后臺數(shù)據(jù)成功但返回不走success而走的error問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
標(biāo)簽:衡陽 德陽 婁底 黔東 樂山 邯鄲 海南 內(nèi)江
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解決ajax提交到后臺數(shù)據(jù)成功但返回不走success而走的error問題》,本文關(guān)鍵詞 解決,ajax,提,交到,后臺,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。