zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    MySQL8.0设置远程访问权限
    MySQL创建用户与授权
    input lable水平对齐
    jquery datagrid加载后仅选定第一行
    jquery隐藏按钮
    C# 添加excel批注
    在ashx中使用Server对象
    jquery 获取datagrid行数
    弹出窗体中加载页面
    【MySQL】MySQL零碎积累
  • 原文地址:https://www.cnblogs.com/-scl/p/7245634.html
Copyright © 2011-2022 走看看