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

前端 未结 6 1140
独厮守ぢ
独厮守ぢ 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:54

    If you are using spring mvc 4 then below will be the best approach

    Jquery code

    var dataArrayToSend = []; dataArrayToSend.push("a"); dataArrayToSend.push("b"); dataArrayToSend.push("c");

    // ajax code

    $.ajax({ contentType: "application/json", type: "POST", data: JSON.stringify(dataArrayToSend), url: "/appUrl", success: function(data) { console.log('done'); }, error: function(jqXHR, textStatus, errorThrown) { console.log('error while post'); }
    });

    Spring controller code

    @RequestMapping(value = "/appUrl", method = RequestMethod.POST) public @ResponseBody void yourMethod(@RequestBody String[] dataArrayToSend) { for (String data : dataArrayToSend) { System.out.println("Your Data =>" + data); } }

    check this helps you or not!

    Cheers!

提交回复
热议问题