Seongwon-Choi
Seongwon-Choi

Reputation: 457

How do I get the values of the model returned from Controller in ajax?

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);
         }
       }
     });
}

I tried to find the way. But it failed.

I want to get the values ​​of the model returned from the controller in ajax and display it in jsp.

But I don't know how.

As a beginner, I do not know much, so I ask a lot of questions. Please let me know if I have provided the information properly. Please let me know if the gist of the question is wrong. I need your opinion.

Upvotes: 1

Views: 2891

Answers (2)

Shantaram Tupe
Shantaram Tupe

Reputation: 1666

Change your return type to List<HashMap<String, Object>> instead of String and return 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();

}

In Ajax Success try to access as

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

Changed method = RequestMethod.POST to method = RequestMethod.GET Hope this help...

Upvotes: 2

Nikolai
Nikolai

Reputation: 785

  1. You don't need using GET for getting data from server.

  2. You don't need using ModelAndView. It is using for create html page by ViewResolver - it takes the form of a String view name and put into the form objects from a model.

You can just return needed data from method:

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

And fix request:

$.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);
     }
   }
 });

Upvotes: 1

Related Questions