【问题标题】:How do I get the values of the model returned from Controller in ajax?如何在 ajax 中获取从 Controller 返回的模型的值?
【发布时间】:2017-08-08 05:20:31
【问题描述】:

AdminController.java

@RequestMapping(value="show_allMember.do",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.POST)
    public @ResponseBody String memberList(ModelAndView mav)
    {
        List<HashMap<String, Object>> members = aService.getMemberList();
        mav.addObject("mList",members);

        System.out.println("model is that : " + mav);

        return "" + mav ;
    }

adminMainPage.jsp

function show_allMember()
{
    $('.member_management_area').css('display','block');
    $('.member_management').css('color','#fe4978');

    $.ajax
    ({
        type: 'POST',
        url: 'http://localhost:8080/rachelivf/show_allMember.do',
        dataType:'JSON',
        success: function(mList)
        {
            console.log(mList);
            $.each(response.mList, function(index, value)
            {
                console.log(response.list);
            });
       },
       error: function(xhr,ajaxOptions,thrownError)
       {
           if(xhr.status == 404)
         {
               alert(thrownError);
         }
       }
     });
}

我试图找到路。但是失败了。

我想在中获取控制器返回的模型的值 ajax并在jsp中显示。

但我不知道怎么做。

作为一个初学者,我知道的不多,所以我问了很多问题。 如果我提供的信息正确,请告诉我。 如果问题的要点有误,请告诉我。 我需要你的意见。

【问题讨论】:

    标签: javascript jquery ajax spring spring-mvc


    【解决方案1】:

    将您的return type 更改为List&lt;HashMap&lt;String, Object&gt;&gt; 而不是String 并返回members

    @RequestMapping(value="show_allMember.do",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.GET)
    public @ResponseBody String memberList(ModelAndView mav)
    {
        List<HashMap<String, Object>> members = aService.getMemberList();
        return members ;
        //OR
        //return aService.getMemberList();
    
    }
    

    在 Ajax Success 中尝试访问为

    type: 'GET',
    success: function(mList)
        {
            console.log(mList);
            $.each(response.mList, function(index, value)
            {
                console.log(value.yourMapKey);
            });
       },
    

    method = RequestMethod.POST 更改为method = RequestMethod.GET 希望对您有所帮助...

    【讨论】:

    【解决方案2】:
    1. 您不需要使用GET 从服务器获取数据。

    2. 您不需要使用ModelAndView。它用于由ViewResolver 创建 html 页面 - 它采用字符串视图名称的形式并从模型放入表单对象中。

    您可以从方法中返回所需的数据:

        @RequestMapping(value="show_allMember.do",produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
        public @ResponseBody List<HashMap<String, Object>> memberList()
        {
            return aService.getMemberList();
        }
    

    并修复请求:

    $.ajax
    ({
        type: 'GET',
        url: 'http://localhost:8080/rachelivf/show_allMember.do',
        dataType:'JSON',
        success: function(response)
        {
            $.each(response, function(index, value)
            {
                console.log(value);
            });
       },
       error: function(xhr,ajaxOptions,thrownError)
       {
           if(xhr.status == 404)
         {
               alert(thrownError);
         }
       }
     });
    

    【讨论】:

    • 成功:在函数(响应)部分,响应中是否包含控制器返回的值?
    • response是控制器返回的值(responce has type List&lt;HashMap&lt;String, Object&gt;&gt;)
    • 很抱歉打扰您,但我不知道如何一对一交谈。如果您走路我将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    相关资源
    最近更新 更多