• Spring MVC ajax传值


     1.传字符串

    @RequestMapping("/t")
    @ResponseBody
    public ResultData t(String name,String pass){
    	Map<String,Object> map = new HashMap<String,Object>() ;
    	map.put("name", name) ;
    	map.put("pass", pass) ;
    	return new ResultData(map) ;
    }
    
    @RequestMapping("/t")
        @ResponseBody
        public ResultData t(@RequestParam("name") String name,@RequestParam("pass") String pass){
            Map<String,Object> map = new HashMap<String,Object>() ;
            map.put("name", name) ;
            map.put("pass", pass) ;
            return new ResultData(map) ;
        }
    <script type="text/javascript">
                $("a").click(function(){
                    $.ajax({
                        url:"web/t",
                        data:{name:"测试",pass:"123"},
                        success:function(data){
                            console.log(data) ;
                        }
                    })
                }) ;
            </script>

    2.传对象到后台

    @RequestMapping("/t")
        @ResponseBody
        public ResultData t(Books book){
            return new ResultData(book) ;
        }
    <script type="text/javascript">
                $("a").click(function(){
                    $.ajax({
                        url:"web/t",
                        data:{name:"神话",writter:"张三"},
                        success:function(data){
                            console.log(data) ;
                        }
                    })
                }) ;
            </script>

    3.传集合或对象数组到后台

        传集合不能用1,2的方式,用json字符串,后台在解析成集合

    @RequestMapping("/t")
        @ResponseBody
        public ResultData t(String books ) {
            String book = books.replaceAll("&quot;", """);
            JSONArray j = JSONArray.fromObject(book) ;
            List<Books> list = j.toList(j,Books.class) ;
            return new ResultData(list) ;
        }
    <script type="text/javascript">
                $("a").click(function(){
                    var books = [{name:"昏天魔王",writter:"南瓜大虾"},{name:"盖世英雄",writter:"真空泡"}] ;
                    console.log(JSON.stringify(books))
                    $.ajax({
                        url:"web/t",
                        data:{books:JSON.stringify(books)},
                        contentType: "application/json; charset=utf-8",    
                        dataType: "json",  
                        success:function(data){
                            console.log(data) ;
                        }
                    }) 
                }) ;
            </script>
  • 相关阅读:
    part of Hypertext Transfer Protocol HTTP/1.1
    Run Windows Service as a console program
    UNION 和UNION ALL 的区别
    分布式拒绝服务攻击(DDoS)原理及防范
    执行存储过程超时 SQL
    sql 小技巧 =》本周五和上周四的时间
    OPENXML with xmlns:dt
    Comparing the Timer Classes in the .NET Framework Class Library
    图片(地图)热点区域高亮显示研究
    用YSlow分析我们页面(转)
  • 原文地址:https://www.cnblogs.com/-scl/p/7245634.html
Copyright © 2020-2023  润新知