Spring MVC ajax传Json值、取返回值(@RequestParam和@RequestBody区别)

前端界面

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
    <script src="../js/jquery-1.11.0.min.js"></script>
    <script>
        $(document).ready(function () {
        })
    </script>
</head>
<body>
<div style="margin: 20% 40%">
    <h3 id="h3cc">功能</h3>
    <input type="button" value="取值触发" id="bt1"/>
    <input type="button" value="传Str值触发" id="bt2"/>
    <input type="button" value="传Obj值触发" id="bt3"/>
</div>
<script>
    $("#bt1").click(function () {
        getUser();
    });
    $("#bt2").click(function () {
        paramJson();
    });
    $("#bt3").click(function () {
        bodyJson();
    });
    function bodyJson(){
        var jsonMessage = [{'id':'00005','gender':'男','name':'张小明','type':'用户'},{'id':'00006','gender':'男','name':'李小明','type':'用户'}];
        $.ajax({
            url : "http://10.0.1.46:8080/springmvc/user/bodyJson",
            type : "post",
            async : true,
            contentType : "application/json",
            data : JSON.stringify(jsonMessage),
            dataType : 'text',
            success : function(data) {
                alert(data);
            },
            error: function () { alert("error"); }
        });
    }
    function paramJson(){
        var jsonMessage = {result:"[{'id':'00005','gender':'男','name':'张小明','type':'用户'},{'id':'00006','gender':'男','name':'李小明','type':'用户'}]"};
        $.ajax({
            url : "http://10.0.1.46:8080/springmvc/user/paramJson",
            type : "post",
            async : true,
            //contentType : "application/json",
            data : jsonMessage,
            dataType : 'text',
            success : function(data) {
                alert(data);
            },
            error: function () { alert("error"); }
        });
    }

    function  getUser() {
        <!--ajax取值-->
        var jsonObject = {
            "userId":"admin",
            "password":1
        };
        var jdata={userId:"admin"};
        //alert(jdata.userId);
        $.ajax({
            url : "http://10.0.1.46:8080/springmvc/user/getUser",
            type : "post",
            async : true,
            //contentType : "application/json",
            data :{userId:"admin"},
            dataType : 'json',
            success : function(rs){
                alert(rs.type);
            },
            error: function () { alert("error"); }
        });
    }



    //type:RequestBody
    /**/
    //type:RequestParam
    /*
   */
</script>
</body>
</html>

后端控制层

//接收复杂JSON测试
@RequestMapping(value = "/paramJson")
@ResponseBody
public String paramJson(@RequestParam String result){
    /*JSONArray arr = (JSONArray) JSONArray.parse(result);
    for(int i=0;i<arr.size();i++){
        System.out.println(arr.get(i).toString());
    }*/
    System.out.println("UserController====paramJson==============================================测试JSON!"+result);

    return "success";
}

//接收复杂JSON测试
@RequestMapping(value = "/bodyJson")
@ResponseBody
public String bodyJson(@RequestBody NewUser[] result){
    /*JSONArray arr = (JSONArray) JSONArray.parse(result);
    for(int i=0;i<arr.size();i++){
        System.out.println(arr.get(i).toString());
    }*/
    System.out.println("UserController====bodyJson==============================================测试JSON!"+result);

    return "success";
}

//根据id获得用户信息
@RequestMapping(value = "/getUser")
@ResponseBody
public User getUsers(@RequestParam String userId) {
    //调用service方法得到用户列表
    User users = userService.selectByPrimaryKey(userId);

    System.out.println("UserController==================================================成功查询用户!"+userId);
    return users;
}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值