前端接收不到后台发来的JSON问题原因

昨天在做一个头像上传的功能,要求选择完成图片以后立刻完成上传功能并且在页面上显示,需要用到异步上传,就是用了ajaxSubmit()进行表单的上传操作,但是不管怎么弄前台都接收不到后端发来的json数据,设置dataType为text倒是能接收到,郁闷了好长时间。
一开始我的json数据是这样的:

"{fileName:picture.jpg}"

后来我认为是自己的json格式有问题,就改成了这样:

"{fileName:\"picture.jpg\"}"

还不行,就改成这样:

"{\"fileName\":\"picture.jpg\"}"

果然,是json的问题,自己蠢了。然而人要有刨根问底的精神,在博客上搜了一下,果然有人也遇到过这样的问题,而由于这样的不严谨的json格式产生BUG的历史是这样的:

“json”: Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)


标准的json格式如下
  1)键名称:用双引号 括起
  2)字符串:用使用双引号 括起
  3)数字,布尔类型不需要 使用双引号 括起
想知道详细戳这里:json在线API

发布了31 篇原创文章 · 获赞 17 · 访问量 4万+
展开阅读全文

前端接收不到后台的值,求大神解决,在线等,解决了速度结贴。。。。。。。。。。。。。。。。。。。。

08-05

package com.togest.emis.modules.g6c.web; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import com.togest.emis.framework.orm.Page; import com.togest.emis.framework.orm.PropertyFilter; import com.togest.emis.framework.utils.ConvertUtils; import com.togest.emis.framework.utils.JSONParserUtils; import com.togest.emis.framework.utils.RequestParamUtils; import com.togest.emis.framework.utils.StringHelper; import com.togest.emis.modules.g6c.entity.Download; import com.togest.emis.modules.g6c.service.Downloadservice; import com.togest.emis.modules.system.cache.SystemCache; import com.togest.emis.modules.system.config.SystemConstants; /** * * @author mmk * * */ @Controller @RequestMapping(value = "/6c/system/download") public class DownloadController { @Autowired private Downloadservice service; @RequestMapping(method = RequestMethod.GET) public String list(Model model, HttpServletRequest request, String id) { model.addAttribute("psn_id", id); model.addAttribute(SystemConstants.NAV_PATH, SystemCache.getPath(id, RequestParamUtils.getUserID(request))); return "/6c/download"; } @RequestMapping(value = "getPage", method = RequestMethod.POST) public void getPage(HttpServletRequest request, HttpServletResponse response, String name, String address ,Model model) { Map<String, Object> map = new HashMap<String, Object>(); try { List<PropertyFilter> filters = PropertyFilter .buildFromHttpRequest(request); Page page = RequestParamUtils .getPage(request, new Page<Download>()); if (!page.isOrderBySetted()) { page.setOrderBy(SystemConstants.OrderByField); page.setOrder(Page.ASC); page = service.findPage(page, filters); } map.put(SystemConstants.EASYTABLE_PAGE_TOTAL_INDEX, page.getTotalCount()); map.put(SystemConstants.EASYTABLE_PAGE_RESULT_INDEX, page.getResult()); } catch (Exception e) { e.printStackTrace(); } try { JSONParserUtils.write(response, JSONObject.fromObject(map) .toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping(value = "save", method = RequestMethod.POST) public void save(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request, Model model,HttpServletResponse response, Download d,Map<String,Object> map) { String path = request.getSession().getServletContext() .getRealPath("download"); String fileName = request.getParameter("name"); String fileContent = file.getOriginalFilename(); String fileType = fileContent.substring(fileContent.lastIndexOf(".")); String fileAll = fileName+fileType; String address = path+"\\"+fileAll; String file_path = address.substring(address.lastIndexOf("download")); String fileUrl = request.getContextPath() + "/download/" + fileAll; // //String fileName = new Date().getTime()+".jpg"; File targetFile = new File(path, fileAll); if (!targetFile.exists()) { targetFile.mkdirs(); } // 保存 try { file.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); } map.put("fileAll", fileAll); model.addAttribute("address", address); model.addAttribute("path", path); model.addAttribute("fileAll", fileAll); model.addAttribute("fileUrl",fileUrl); model.addAttribute("fileName", fileName); System.out.println("+++++++++++++++"+ fileAll); if (StringHelper.isBlank(d.getId())) { d.setId(null); } //d.setFile(fileAll); d.setAddress(file_path); service.save(d); map = new HashMap<String, Object>(); map.put("msg", "新增成功"); try { JSONParserUtils.write(response, JSONObject.fromObject(map) .toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping(value = "edit", method = RequestMethod.GET) public void edit(HttpServletRequest request, HttpServletResponse response) { String id = RequestParamUtils.getStringParamter(request, "id"); if (id != null) { Download d = service.get(id); try { Map<String, Object> map = ConvertUtils.objectToMap(d, new String[] { "id", "sort", "name", "address" }); JSONObject json = JSONObject.fromObject(map); JSONParserUtils.write(response, json.toString()); } catch (Exception e) { e.printStackTrace(); } } } @RequestMapping(value = "update", method = RequestMethod.POST) public void update(HttpServletResponse response, Download d) { service.save(d); Map<String, Object> map = new HashMap<String, Object>(); map.put("msg", "修改成功"); try { JSONParserUtils.write(response, JSONObject.fromObject(map) .toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping(value = "delete", method = RequestMethod.POST) public void delete(HttpServletResponse response, String id) { Map<String, Object> map = new HashMap<String, Object>(); String[] ids = id.split(","); service.delete(id); map.put("msg", "删除成功"); try { JSONParserUtils.write(response, JSONObject.fromObject(map) .toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping(value = "find", method = RequestMethod.POST) public String find(Model model, HttpServletRequest request) { List<Download> dlist = service.findAll(); // for(Download d :dlist){ // String dname =d.getName(); // String daddress = d.getAddress(); // request.setAttribute("dname", dname); // request.setAttribute("daddress", daddress); // } model.addAttribute("dlist", dlist); // ActionContext.getContext().getSession().put("dlist", dlist); // ActionContext.getContext().getValueStack().set("dlist", dlist); // request.setAttribute("dlist", dlist); return "/system/passport/login"; } } 这是控制器的代码 package com.togest.emis.modules.g6c.service; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.togest.emis.framework.orm.Page; import com.togest.emis.framework.orm.PropertyFilter; import com.togest.emis.modules.g6c.dao.DownloadDao; import com.togest.emis.modules.g6c.entity.Download; import com.togest.emis.modules.system.entity.P_Dictionary; @Service public class Downloadservice { @Autowired private DownloadDao dao; /** * 根据主键ID删除实体 * * @param id */ public void delete(String id) { dao.delete(id); } /** * 保存实体 * * @param entity */ public void save(Download entity) { dao.save(entity); } /** * 根据主键ID获取实体 * * @param id * @return */ public Download get(String id) { return dao.get(id); } /** * 查询所有 */ public List<Download> findAll(){ String hql ="FROM Download"; List<Download> list = dao.find(hql); //for(Download d : list){ //System.out.println("+++++++++++++"+d.getName()); //} return list; } public Page<Download> findPage(final Page<Download> page, final List<PropertyFilter> filters) { return dao.findPage(page, filters); } } 业务层代码 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="/common/taglibs.jsp"%> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览