主页 > 知识库 > 解决ajax提交到后台数据成功但返回不走success而走的error问题

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

热门标签:科大讯飞语音识别系统 服务器配置 Linux服务器 银行业务 团购网站 阿里云 Mysql连接数设置 电子围栏

下面是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;
 
 //批量导入。参数:文件名,文件。
 ListExcelName> 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问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
  • Ajax请求发送成功但不进success的解决方法
  • 解决ajax请求后台,有时收不到返回值的问题
  • 解决AJAX返回状态200没有调用success的问题

标签:大理 衡水 枣庄 萍乡 江苏 广元 衢州 蚌埠

巨人网络通讯声明:本文标题《解决ajax提交到后台数据成功但返回不走success而走的error问题》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266