Java 在 ajax spring mvc 中返回 ModelAndView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19976297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning ModelAndView in ajax spring mvc
提问by Monicka Akilan
Hi am using spring mvc + ajax. I made a ajax call by passing a userid. And everything goes fine successfully returned to ajax but when i alert the response its simple showing the html page code. Please help me to sort out this prob. I think i didnt coded my ajax well.Help me to in correct way
嗨,我正在使用 spring mvc + ajax。我通过传递用户 ID 进行了 ajax 调用。一切顺利,成功返回到 ajax,但是当我提醒响应时,它简单地显示了 html 页面代码。请帮我解决这个问题。我想我没有很好地编码我的 ajax。帮助我以正确的方式
Controller code:
控制器代码:
public @ResponseBody ModelAndView abc(HttpServletRequest httpServletRequest,
HttpSession session, ModelMap map){
ModelAndView modelAndView = new ModelAndView("abcd.page",
"commandName", object);
return modelAndView;
Ajax code :
阿贾克斯代码:
$(".userDetails").click(function() {
alert("clicked");
var userId=$(this).parent().parent(). parent().find(".userId").
text().trim();
alert("userId :"+userId);
$.ajax({
url : 'ABC.htm',
type : 'GET',
data: {userId:userId},
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
},
success : function(response) {
alert("success");
alert(response);
},
error : function(res) {
alert("error");
},
});
return false;
});
The output for the alert(response); is

警报(响应)的输出;是

EDIT:Can any one please tell why ajax giving html content on success... After many changes i made getting the same alert.
编辑:任何人都可以告诉为什么 ajax 在成功时提供 html 内容......经过多次更改后,我收到了相同的警报。
Edited Again :I think i dont have any problem in controller. Please suggest me solution to code my ajax correctly. It seems error here. How to get a ModelAndView object in ajax
再次编辑:我认为我在控制器中没有任何问题。请建议我正确编码我的ajax的解决方案。这里似乎是错误的。如何在ajax中获取ModelAndView对象
回答by Sotirios Delimanolis
You don't get a ModelAndViewobject in AJAX. Spring uses HandlerMethodReturnValueHandlerinstances to handle your handler method's return value. For ModelAndView, it uses ModelAndViewResolverMethodReturnValueHandler. For @ResponseBody, it uses RequestResponseBodyMethodProcessor. These are checked in a specific order and the one for ModelAndViewhas higher priority. Therefore, when you return a ModelAndView, Spring will add the model attributes to the full Modeland then resolve your view name to, probably, a jspand write the response from that jsp, giving you some HTML. Since AJAX just sees the response from the request, it will see HTML.
你ModelAndView在 AJAX 中没有得到对象。Spring 使用HandlerMethodReturnValueHandler实例来处理处理程序方法的返回值。对于ModelAndView,它使用ModelAndViewResolverMethodReturnValueHandler. 对于@ResponseBody,它使用RequestResponseBodyMethodProcessor. 这些按特定顺序检查,其中一个ModelAndView具有更高的优先级。因此,当您返回 a 时ModelAndView,Spring 将完整添加模型属性Model,然后将您的视图名称解析为 ajsp并写入来自该的响应jsp,为您提供一些 HTML。由于 AJAX 只看到来自请求的响应,因此它会看到 HTML。
If you want to return JSON, don't return a ModelAndView, return the model object directly or write JSON directly to the response yourself.
如果要返回JSON,不要返回a ModelAndView,直接返回模型对象或者自己直接写JSON到响应中。

