I am trying to hit my SpringBoot controller with a JSON and for that I am using AJAX. I want my controlelr to receive the AJAX call, extract the JSON, works with the values and return a JSON response back to the script which I will then utilize in some way.
I am not able to figure out how to code my controller so as to handle the AJAX and also if the request should be POST or GET ?
Here is my script code:
<script>
database.on('child_added', function (snapshot) {
var data = {};
data["FirstName"] = snapshot.val().FirstName;
data["LastName"] = snapshot.val().LastName;
data["Number"] = snapshot.val().Number;
$.ajax({
type: "GET",
contentType: "application/json",
url:"my-localhost/application/print",
data: JSON.stringify(data),
dataType: 'json',
cache: false,
success: function(){
console.log("Successfully sent payload")
},
error: function(e){
console.log("Error": , e)
}
});
</script>
Here is my controller for now. I dont know how and what to change in it and how to send the response back to the script:
@RestController
@RequestMapping("/application")
public class AppController
{
@GetMapping("/print")
public void print()
{
System.out.println("Hello World");
}
}