zoukankan      html  css  js  c++  java
  • ajax post请求json数据在spring-controller解析

    1、前端post请求数据:

    userInfo=[{"id":"5","uname":"小李","phone":"guizhou","address":"guizhou","header":"null","motto":"baskteball","nickname":"ku","hobbies":"null"},{"id":"6","uname":"小花","phone":"guizhou","address":"guizhou","header":"null","motto":"baskteball","nickname":"ku","hobbies":"null"}], 
    $.ajax({
            url:"user/saveAmend",
            type:"post",
            data:{
                "userList":JSON.stringify(userInfo)
            },
            // contentType: 'application/json;charset=utf-8',
            //traditional:true,
            //dataType: "json",
            success:function () {
                // var dt = JSON.parse(result);
                console.log("ok")
            },
            errorr:function () {
                console.log("服务器出现异常!")
            }
        })
    

      

    2、在contorller里面解析 

    @RequestMapping(value = "/saveAmend",method = RequestMethod.POST)
        //@ResponseBody
        public void saveAmend(@RequestParam(value = "userList") String userList,
                              HttpServletResponse response) throws IOException {
            //修改接收的内容。
            System.out.println(userList);
    
            List<Map<String,String>> uList = (List<Map<String, String>>) JSONObject.parse(userList);
            System.out.println(uList.size());
    
    
    //        ObjectMapper mapper = new ObjectMapper();
    //        response.getWriter().write(mapper.writeValueAsString(true));
    //        response.getWriter().close();
    //        return "success";
        }
    

      

    3、在配置maven的 pom.xml里面导入阿里json jar包

    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.56</version>
        </dependency>
    

      

      最后我想说一句日了狗了,这个问题搞了我一早上。

  • 相关阅读:
    jQuery动态加载动画spin.js
    jQuery自动过滤单词插件
    基于jQuery的自定义滚动条
    jQuery纵向分类下拉菜单导航
    仿酷狗官网新闻焦点图插件
    metro扁平UI网页组件
    HTML5环形音乐播放器
    纯CSS3个性化圆形按钮登录表单
    纯CSS3绘制的黑色图标按钮组合
    纯CSS3实现iOS7扁平化图标
  • 原文地址:https://www.cnblogs.com/wuzaipei/p/10619922.html
Copyright © 2011-2022 走看看