关于ajax请求方法到后台返回的数据类型处理

ajax的dataType问题

  1. SpringMVC方法返回一个字符串时,前端的ajax的dataType若指定json,即便ajax请求成功,也不进success的回调;
  2. SpringMVC方法返回一个字符串时,前端的ajax的dataType若不指定json,则ajax会把后端返回的字符串直接传给success的回调函数参数data;
  3. SpringMVC方法返回一个JSON字符串时,前端的ajax的dataType若指定json,则ajax会把后端返回的json字符串自动转换成json对象,传给success的回调函数参数data;
  4. SpringMVC方法返回一个JSON字符串时,前端的ajax的dataType若不指定json,则ajax会把后端返回的json字符串直接赋值给success的回调函数参数data;我们需要在success中使用JSON.parse()方法手动转换;
  5. SpringMVC方法返回一个JSON对象时,前端的ajax的dataType若指定json,即便ajax请求成功,也不进success的回调;
  6. SpringMVC方法返回一个JSON对象时,前端的ajax的dataType若不指定json,则ajax会把SpringMVC解析回的页面直接赋值给success的回调函数参数data,即data是一个文档对象,代表页面;
  7. SpringMVC方法返回一个Map对象时,前端的ajax的dataType指不指定json可以,ajax会把SpringMVC返回的Map集合直接赋值给success的回调函数参数data(注意:需要在SpringMVC的方法上使用@ResponseBody注解);

返回success之后data数据的处理方式:

  1. 请求方法返回值是Map
@RequestMapping(value = "queryOaTempTech")
@ResponseBody
public Map<String, Object> queryOaTempTech(HttpServletRequest request, HttpServletResponse response) {Map<String, Object> cusTechNumMap = new HashMap<String, Object>();try {String cusTechNums = request.getParameter("cusTechNums").toString().trim();if (!StringUtils.isRealEmpty(cusTechNums)) {OaTempTech temptech = new OaTempTech();if (cusTechNums.contains("|")) {String[] cusTechNumes = cusTechNums.split("\\|");for (String str : cusTechNumes) {if (!StringUtils.isRealEmpty(str)) {temptech.setCusTechNum(str);List<OaTempTech> list = oaTempTechService.findList(temptech);if (list.size() > 0) {OaTempTech temptech2 = list.get(0);// 如果DCC审核意见没有填写,那么就认为流程没有走过这个节点// 走过这个节点,后面的流程就是MES工程变更执行和结束String sug = temptech2.getSug2();if (StringUtils.isRealEmpty(sug)) {String userName = temptech2.getUser().getName();cusTechNumMap.put(temptech2.getCusTechNum(), "未在临技单流程执行完毕,请联系临技单申请人" + userName);}} else {cusTechNumMap.put(str, "未在临技单流程中找到,请确认输入是否正确");}}}} else {temptech.setCusTechNum(cusTechNums);List<OaTempTech> list = oaTempTechService.findList(temptech);if (list.size() > 0) {OaTempTech temptech2 = list.get(0);// 如果DCC审核意见没有填写,那么就认为流程没有走过这个节点// 走过这个节点,后面的流程就是MES工程变更执行和结束String sug = temptech2.getSug2();if (StringUtils.isRealEmpty(sug)) {String userName = temptech2.getUser().getName();cusTechNumMap.put(temptech2.getCusTechNum(), "未在临技单流程执行完毕,请联系临技单申请人" + userName);}} else {cusTechNumMap.put(cusTechNums, "未在临技单流程中找到,请确认输入是否正确");}}}} catch (Exception e) {e.printStackTrace();}if (cusTechNumMap.keySet().size() == 0) {return null;} else {return cusTechNumMap;}
}

页面处理方案:(for (var key in data) { })

success: function (data) {if (data != null) {var messages = ""for (var key in data) {var mess = '【' + '客户临技单号' + key + data[key] + '】' + '\n';messages += mess;}flag = false;top.$.jBox.tip(messages, "info", {persistent: true, opacity: 0, timeout: 5000});return false;}
}
  1. 请求方法返回值是Map
@RequiresPermissions("production:productionorder:scProductionOrderSubject:edit")
@RequestMapping(value = "queryByPackageName")
@ResponseBody
public Map<String, Object> queryByPackageName(HttpServletRequest request, HttpServletResponse response, Model model)throws JSONException {Map<String, Object> mapList = new HashMap<String, Object>();try {// 获取输入的资料包名称String packName = request.getParameter("packageName");if (!StringUtils.isBlank(packName) && !"".equals(packName)) {// 获取资料包主表相关信息ScProductionPackageTitle packageTitle = new ScProductionPackageTitle();packageTitle.setPackageName(packName);/*************** 因为是最新的版本的一个资料包, 所以 状态是审核通过的情况下面,版本号是最大的,set这两个条件进行查询***********/// 是否通过审核状态(0通过,1未通过)packageTitle.setApproved("0");List<ScProductionPackageTitle> packageTitleList = scProductionPackageTitleService.findNewestPackageName(packageTitle);if (packageTitleList.size() < 1) {mapList.put("packageTitleList", "");} else {mapList.put("packageTitleList", packageTitleList);}}} catch (Exception e) {e.printStackTrace();}return mapList;
}

页面处理方案:(for(var i=0;i

success:function(data){if(data.packageTitleList.length == 0){/* 没有相关新版记录 */top.$.jBox.tip("", "info", {persistent: true, opacity: 0, timeout: 5000});$("#packetVersionNew").val("");return false;}else{//显示所有新版本的资料包名称var num = 0;/* 生产资料包明细/DataPackageDetails */var tableContent = "";for(var i=0;i<data.packageTitleList.length;i++){num = num +1 ; var titleId = data.packageTitleList[i].id;var packageName = data.packageTitleList[i].packageName;var titleVersion = data.packageTitleList[i].versionInformation;var titleName = packageName + "_" + titleVersion;var titleFalg = 0;/* 新加生产通知书选择资料包的时候需要保存资料包的ECN、其他流程实例ID(otherProcInsId) */var ecrNo = data.packageTitleList[i].ecrNo;var ecnNo = data.packageTitleList[i].ecnNo;var otherProcInsId = data.packageTitleList[i].otherProcInsId;var oaCallType = data.packageTitleList[i].oaCallType;tableContent += ' + num + '\">';tableContent += '' + data.packageTitleList[i].packageName + '+' + data.packageTitleList[i].versionInformation + ''+"\""/* 查看 */+ 'οnclick=\"queryCheckPackage(\'' + titleId +'\',\'' + titleName +'\',\'' + titleFalg +'\',\'' + ecrNo +'\',\''+ ecnNo + '\',\''+ otherProcInsId + '\',\''+ oaCallType +'\')\">'+' + num + '\" type=\"text\" value= \"' + data.packageTitleList[i].id + '\">';}$("#packetVersionHtmlAppend").after(tableContent); } 
}
  1. 请求方法返回值是List>:
@RequiresPermissions("production:productionorder:scProductionOrderSubject:edit")
@RequestMapping(value = "queryByErp")
@ResponseBody
public List<Map<String, String>> queryByErp(HttpServletRequest request, HttpServletResponse response, Model model)throws JSONException, SQLException {List<Map<String, String>> list = new ArrayList<Map<String, String>>();PreparedStatement pstmt = null;Connection conn = null;ResultSet rs = null;try {StringBuffer sql = new StringBuffer("");if (!StringUtils.isBlank(singleAccount) && !"".equals(singleAccount)) {conn = getConnection(singleAccount);if (table.equals("dcsibb")) {// 查询 DCSIBB 表sql.append("SELECT * FROM DCSIBB WHERE IBB023='T' AND IBB015='N'");if (!StringUtils.isBlank(ibbOne)) {sql.append(" AND IBB001 LIKE '%" + ibbOne + "%'");}if (!StringUtils.isBlank(ibbTow)) {sql.append(" AND IBB002 LIKE '%" + ibbTow + "%'");}} pstmt = (PreparedStatement) conn.prepareStatement(sql.toString());rs = pstmt.executeQuery();while (rs.next()) {if (table.equals("dcsibb")) {String one = rs.getString("IBB001");String tow = rs.getString("IBB002");String three = rs.getString("IBB003");String four = rs.getString("IBB004");String fortyOne = rs.getString("IBB041");String five = rs.getString("IBB005");String six = rs.getString("IBB006");String fourteen = rs.getString("IBB014");String thirteen = rs.getString("IBB013");Map<String, String> mapList = new HashMap<String, String>();mapList.put("IBB001", one);mapList.put("IBB002", tow);mapList.put("IBB003", three);mapList.put("IBB004", four);mapList.put("IBB041", fortyOne);mapList.put("IBB005", five);mapList.put("IBB006", six);mapList.put("IBB014", fourteen);mapList.put("IBB013", thirteen);list.add(mapList);} }conn.close();}} catch (Exception e) {e.printStackTrace();} finally {rs.close();pstmt.close();}return list;
}

页面处理方案:(for(i=0;i

success:function(data){if(data != null){var context=""dataList = data; //集合赋值for(i=0;i<data.length;i++){var cont ="";cont +="";cont +=""+ data[i].IBB001+"";cont +=""+ data[i].IBB002+"";cont +=""+ data[i].IBB003+"";cont +=""+ data[i].IBB004+"";cont +=""+ data[i].IBB041+"";cont +=""+ data[i].IBB005+"";cont +=""+ data[i].IBB006+"";cont +=""+ data[i].IBB014+"";cont +=""+ data[i].IBB013+"";context += cont;}$("#met_body").html(context);    } 
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部