主頁(yè) > 知識(shí)庫(kù) > 解決ajax提交到后臺(tái)數(shù)據(jù)成功但返回不走success而走的error問(wèn)題

解決ajax提交到后臺(tái)數(shù)據(jù)成功但返回不走success而走的error問(wèn)題

熱門標(biāo)簽:科大訊飛語(yǔ)音識(shí)別系統(tǒng) 服務(wù)器配置 Linux服務(wù)器 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 阿里云 Mysql連接數(shù)設(shè)置 電子圍欄

下面是ajax代碼和Controller層代碼,期初以為是后臺(tái)程序?qū)戝e(cuò)了。

 $("#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)跟蹤后臺(tái)發(fā)現(xiàn),原來(lái)因?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="請(qǐng)勿重復(fù)提交" type="button" value="上 傳" onClick="submitImport()">
 input style="margin-left: 20px;" type="submit" value="下載模板" onClick="downloadTemplate();">
 /form>
 /div>
/body>
/html>

以上這篇解決ajax提交到后臺(tái)數(shù)據(jù)成功但返回不走success而走的error問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Ajax請(qǐng)求發(fā)送成功但不進(jìn)success的解決方法
  • 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問(wèn)題
  • 解決AJAX返回狀態(tài)200沒有調(diào)用success的問(wèn)題

標(biāo)簽:大理 衡水 棗莊 萍鄉(xiāng) 江蘇 廣元 衢州 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解決ajax提交到后臺(tái)數(shù)據(jù)成功但返回不走success而走的error問(wèn)題》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266