在阿贾克斯的Spring MVC返回的ModelAndView [英] Returning ModelAndView in ajax spring mvc

查看:1052
本文介绍了在阿贾克斯的Spring MVC返回的ModelAndView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我使用的Spring MVC + AJAX。我传递一个用户ID做了一个Ajax调用。而一切顺利成功地回到了阿贾克斯,但是当我提醒响应其简单的显示HTML网页code。请帮我这个概率理清。 我觉得我没有codeD我的ajax well.Help我以正确的方式

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

控制器code:

  public @ResponseBody ModelAndView abc(HttpServletRequest httpServletRequest,
        HttpSession session, ModelMap map){

      ModelAndView modelAndView = new ModelAndView("abcd.page",
                "commandName", object);
           return modelAndView;

阿贾克斯code:

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

编辑:任何一个可以告诉为什么成功的AJAX给HTML内容...许多变化我做得到同样的警告后,

Can any one please tell why ajax giving html content on success... After many changes i made getting the same alert.

编辑还是那句话:我觉得我没有在控制器的任何问题。请给我建议的解决方案,以code我的ajax正确。它在这里似乎错误。如何获得一个ModelAndView对象AJAX

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一个的ModelAndView 对象。 Spring使用 HandlerMethodReturnValueHandler 实例来处理你的处理方法的返回值。对于的ModelAndView ,它使用了 ModelAndViewResolverMethodReturnValueHandler 。对于 @ResponseBody ,它使用了 RequestResponseBodyMethodProcessor ​​。这些检查在一个特定的顺序和一个用于的ModelAndView 具有更高的优先级。因此,当您返回的ModelAndView ,Spring将添加模型属性充分型号,然后解决您的视图名称到,很可能,一个的jsp ,写从响应的jsp ,给你一些HTML。由于AJAX只是看到从请求的响应,它会看到HTML

You don't get a ModelAndView object in AJAX. Spring uses HandlerMethodReturnValueHandler instances 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 ModelAndView has higher priority. Therefore, when you return a ModelAndView, Spring will add the model attributes to the full Model and then resolve your view name to, probably, a jsp and write the response from that jsp, giving you some HTML. Since AJAX just sees the response from the request, it will see HTML.

如果你想返回JSON,不返回的ModelAndView ,直接返回的模型对象或直接写信JSON给自己的反应。

If you want to return JSON, don't return a ModelAndView, return the model object directly or write JSON directly to the response yourself.

这篇关于在阿贾克斯的Spring MVC返回的ModelAndView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆