I'm trying to develop simple application using spring mvc and I need to pass javascript parameters to spring controller. I have tried several methods and none of them were worked. Following is my javascript and spring controller. Please help me to sort this issue.
Java script
function searchViaAjax(id) {
alert(id);
$.ajax({
type : "POST",
contentType : "application/json",
url : "search/api/getSearchResult",
data : JSON.stringify(id),
dataType : 'json',
timeout : 100000,
success : function(id) {
console.log("SUCCESS: ", id);
display(id);
alert(response);
},
error : function(e) {
console.log("ERROR: ", e);
display(e);
},
done : function(e) {
console.log("DONE");
}
});
}
AjaxController.java
@Controller
public class AjaxController {
@ResponseBody
@RequestMapping(value = "/search/api/getSearchResult")
public String getSearchResultViaAjax(@RequestParam(value = "id") int id) {
System.out.println("come to ajax"+ id);
return "hello";
}
}
data : JSON.stringify({ id: id }),