admin管理员组文章数量:1323342
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("e to ajax"+ id);
return "hello";
}
}
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("e to ajax"+ id);
return "hello";
}
}
Share
Improve this question
asked Aug 21, 2017 at 7:05
dmaprasaddmaprasad
3114 gold badges7 silver badges17 bronze badges
2
-
data : JSON.stringify({ id: id }),
– Ivan Minakov Commented Aug 21, 2017 at 7:10 - I tried in this way. But didn't work for me – dmaprasad Commented Aug 21, 2017 at 7:24
3 Answers
Reset to default 3When you pass json as requestbody then above call is applicable. To send request param you have to as below:
Use following ajax call:
function searchViaAjax(id) {
var tempId = id;
$.ajax({
type : "POST",
url : "/search/api/getSearchResult",
data : {id:tempId},
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");
}
});
}
also you can acheive this using get method as below:
function searchViaAjax(id) {
$.ajax({
type : "GET",
url : "/search/api/getSearchResult/"+id,
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");
}
});
}
@Controller
public class AjaxController {
@ResponseBody
@RequestMapping(value = "/search/api/getSearchResult/{id}")
public String getSearchResultViaAjax(@PathVariable(value = "id") Integer id)
{
return String.valueOf(id);
}
}
When you use JSON.stringify
, you are actually sending a JSON object to your spring controller's method. All you have to do is wrap your json object as a java object like so.
public class UserId {
private int id;
// setters and getters
}
And in your controller's method use @RequestBody
to map your JSON
to your UserId
class like so
@ResponseBody
@RequestMapping(value = "/search/api/getSearchResult", method = RequestMethod.POST)
public String getSearchResultViaAjax(@RequestBody UserId user) {
System.out.println("e to ajax" + user.getId());
return "hello";
}
PS: Haven't tested it but should work.
It is a post request you are making from your ajax to controller. You are missing "methodType=RequestMethod.POST" on your spring controller.
Replace @RequestMapping(value = "/search/api/getSearchResult")
with
@RequestMapping(value = "/search/api/getSearchResult", methodType=RequestMethod.POST)
本文标签: javaSend javascript variables to spring controllerStack Overflow
版权声明:本文标题:java - Send javascript variables to spring controller - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742132939a2422243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论