我如何通过Ajax从Spring Controller获取数据?

ajaxjsp页面上有一个通过URL调用spring控制器的页面/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}",但它显示为空白。

有什么方法可以true/false在查看页面上接收该数据?

回答:

您必须为Spring Ajax调用示例添加@ResponseBody批注

@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获取数据? 的全部内容, 来源链接: utcz.com/qa/398090.html

回到顶部