我如何通过Ajax从Spring Controller中获取数据? [英] How can I get data from spring controller by ajax?
本文介绍了我如何通过Ajax从Spring Controller中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在jsp
页面上有一个ajax
,该页面通过URL /check
调用spring控制器.
I have an ajax
on a jsp
page which calls a spring controller through URL /check
.
$.ajax({
type : "GET",
url : "${pageContext.request.contextPath}/check",
data : {
"id" : ${articleCount}
},
success: function(data){
//response from controller
}
});
现在,控制器看起来像
@RequestMapping("/check")
public String check(@RequestParam Integer id, HttpServletRequest request,
HttpServletResponse response, Model model) {
boolean a = getSomeResult();
if (a == true) {
model.addAttribute("alreadySaved", true);
return view;
} else
model.addAttribute("alreadySaved", false);
return view;
}
我使用模型发送数据,并尝试在success: function(data)
中以"${alreadySaved}"
身份访问它,但显示空白.
I sent data using model and tried to access it in success: function(data)
as "${alreadySaved}"
but it shows blank.
我可以通过任何方式在视图页面上接收true/false
数据吗?
Is there any way I can receive that true/false
data on the view page?
推荐答案
您必须为Spring Ajax调用示例添加@ResponseBody批注
You have to add the @ResponseBody annotation for spring ajax calls example
@RequestMapping("/check")
@ResponseBody
public String check(@RequestParam Integer id, HttpServletRequest request, HttpServletResponse response, Model model) {
boolean a = getSomeResult();
if (a == true) {
model.addAttribute("alreadySaved", true);
return view;
} else {
model.addAttribute("alreadySaved", false);
return view;
}
}
这篇关于我如何通过Ajax从Spring Controller中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文