Sending an Ajax request to a Servlet. Question: How on a servlet to read data from this request and shove, say, the value of the Group variable in String (java)?

function () { var group = $("#group").val(); var request = {}; request.group = group; $.ajax({ /////////////////////// type: "POST", url: "/first", dataType: "json", data: request, success: function (response) { console.log(response.key); var url = "http://localhost:8080/first"; window.open(url); } }); //////////////// } 
  • ajax should be sent to the controller, on the controller to accept the object that describes json - Bohdan Korinnyi
  • In the usual way. String group = request.getParameter ("group"). Ajax request is not distinguishable from the usual request. - Sergey

1 answer 1

Ajax request

 $.ajax({ url:"/send", type:"POST", dataType: 'json', accepts: "application/json", cache: false, async: true, contentType: 'application/json', mimeType: 'application/json', data : {"id":1, "name":"Test"} ... }); 

Java object

 public class User { private int id; private String name; //getter setter } 

Controller

 @Controller public class MainController { @RequestMapping(value="/send", method=RequestMethod.POST) public void send(@RequestBody User User){ } }