Pass array data from javascript in browser to spring mvc controller using ajax

前端 未结 6 1145
独厮守ぢ
独厮守ぢ 2020-12-07 19:27

I would like to pass an array from javascript in web browser to a Spring MVC controller using AJAX

In javascript, I have

var a = [];
a[0] = 1;
a[1] =         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-07 19:48

    Fully tested solution

    $.ajax({
        type : "POST",
        url : "/myurl",
        data : {
            myArray: a //notice that "myArray" matches the value for @RequestParam
                       //on the Java side
        },
        success : function(response) {
           // do something ... 
        },
        error : function(e) {
           alert('Error: ' + e);
        }
    }); 
    
    @RequestMapping(value = "/save/", method = RequestMethod.POST)
        public String controllerMethod(@RequestParam(value="myArray[]") List myArray){
            System.out.println("My Array"+myArray.get(0));
            return "";
        }
    

提交回复
热议问题