溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何解決ajax提交到后臺數(shù)據(jù)成功但返回不走success而走的error問題

發(fā)布時間:2021-07-21 09:56:39 來源:億速云 閱讀:338 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了如何解決ajax提交到后臺數(shù)據(jù)成功但返回不走success而走的error問題,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

下面是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ù):文件名,文件。
 List<ExcelName> cpolicyList = ExcelUtils.batchImport(name,sourceFile);
 //迭代添加信息(注:實際上這里也可以直接將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),原來因為上傳按鍵type寫成了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  id="source_file" name="sourceFile" type="file" value="選擇文件" />
 <input  data-loading-text="請勿重復(fù)提交" type="button" value="上 傳" onClick="submitImport()">
 <input  type="submit" value="下載模板" onClick="downloadTemplate();">
 </form>
 </div>
</body>
</html>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何解決ajax提交到后臺數(shù)據(jù)成功但返回不走success而走的error問題”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI