当前位置: 移动技术网 > IT编程>脚本编程>Ajax > 解决ajax提交到后台数据成功但返回不走success而走的error问题

解决ajax提交到后台数据成功但返回不走success而走的error问题

2018年08月18日  | 移动技术网IT编程  | 我要评论

供销e家休闲农业,gogobox下载,叶倩云

下面是ajax代码和controller层代码,期初以为是后台程序写错了。

 $("#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");
 //进一步判断文件是否为空(即判断其大小是否为0或其名称是否为null)
 long size =sourcefile.getsize();
 if (name==null ||("").equals(name) && size==0) return null;
 
 //批量导入。参数:文件名,文件。
 list<excelname> cpolicylist = excelutils.batchimport(name,sourcefile);
 //迭代添加信息(注:实际上这里也可以直接将cpolicylist集合作为参数,在mybatis的相应映射文件中使用foreach标签进行批量添加。)
 for( excelname customer:cpolicylist){
 	coldataservice.insertdata(customer);
 } 
 
 excelname e1=new excelname();
 e1.setcol0("success");
 return e1;
}

后打点跟踪后台发现,原来因为上传按键type写成了submit导致提交了一次action,致使ajax未获取到返回结果走了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( '导入文件不能为空!'); 
  return; 
 } 
 
 if (epath.substring(epath.lastindexof(".") + 1).tolowercase()!="xlsx") { 
  alert( '导入文件类型必须为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="请勿重复提交" type="button" value="上 传" onclick="submitimport()">
 <input style="margin-left: 20px;" type="submit" value="下载模板" onclick="downloadtemplate();">
 </form>
 </div>
</body>
</html>

以上这篇解决ajax提交到后台数据成功但返回不走success而走的error问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网