ajax springMVC 传递对象

4 篇文章 0 订阅

实体类

一定要实现serializable!

public class User implements Serializable{

    private String username;
    private String password;
    private Integer age;
    private List<String> contacts;
    private Map<String,String> maps;
    //set get toString....

控制层


@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/testAjax1")
    public @ResponseBody User testAjax1(@RequestBody User user){
        // 客户端发送ajax的请求,传的是json字符串,后端把json字符串封装到user对象中
        System.out.println(user);
        // 做响应,模拟查询数据库
        user.setUsername("xxx");
        user.setAge(100);
        user.setPassword("xxx");
        // 做响应
        return user;
    }
        /**
     * 模拟异步请求响应
     */
    @RequestMapping(value="/testAjax2",method = RequestMethod.GET)
    public @ResponseBody User testAjax2(String name,String password){

        // 客户端发送ajax的请求,传的是json字符串,后端把json字符串封装到user对象中
        // 做响应,模拟查询数据库
         User user=new User();
         user.setPassword(password);
         user.setUsername(name);

        // 做响应
        return user;
    }

    /**
     * 模拟异步请求响应
     */
    @RequestMapping(value="/testAjax3",method = RequestMethod.POST)
    public @ResponseBody User testAjax3(String name,String password){

        // 客户端发送ajax的请求,传的是json字符串,后端把json字符串封装到user对象中
        // 做响应,模拟查询数据库
        User user=new User();
        user.setPassword(password);
        user.setUsername(name);
        // 做响应
        return user;
    }
}

jsp页面(传递字符串)

            $("#btn").click(function () {
                // 发送ajax请求
                $.ajax({
                    // 编写json格式,设置属性和值
                    url: "user/testAjax1",
                    contentType: "application/json;charset=UTF-8",
                    data: '{"username":"hehe","password":"123","age":30}',
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        // data服务器端响应的json的数据,进行解析
                        alert(data);
                        alert(data.username);
                        alert(data.password);
                        alert(data.age);
                    }
                });

            });

jsp页面(传递对象)

            $("#btn4").click(function () {
                var user = {
                    "username": "hehe",
                    "password": "123",
                    "age": 10,
                    "contacts": ["123", "456"],
                    "maps": {"key1": "value1", "key2": "value2"}
                }
                alert(4);
                // 发送ajax请求
                $.ajax({
                    // 编写json格式,设置属性和值
                    url: "user/testAjax1",
                    data: JSON.stringify(user),
                    dataType: "json",
                    type: "post",
                    contentType: "application/json",
                    success: function (data) {
                        // data服务器端响应的json的数据,进行解析
                        for (var p in data.maps) {
                            alert(p);
                            alert(data.maps[p]);
                        }
                    },
                    error: function () {
                        alert("error!")
                    }
                });
            });

jsp页面,post/get方式传递多个参数

     $("#btn2").click(function () {
                // 发送ajax请求
                $.ajax({
                    // 编写json格式,设置属性和值
                    url: "user/testAjax2?name=jack&password=123",
                    dataType: "text",
                    type: "get",
                    success: function (data) {
                        // data服务器端响应的json的数据,进行解析
                        alert(data);
                    },
                    error: function () {
                        alert("error!")
                    }
                });

            });
            $("#btn3").click(function () {
                // 发送ajax请求
                $.ajax({
                    // 编写json格式,设置属性和值
                    url: "user/testAjax3",
                    dataType: "text",
                    type: "post",
                    data: "name=jack&password=123",//请求参数
                    success: function (data) {
                        // data服务器端响应的json的数据,进行解析
                        alert(data);
                    },
                    error: function () {
                        alert("error2!")
                    }
                });
            });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值