0

I'm sending ajax request to spring controller and getting array list. How to get ArrayList to ajax result and print it in tabular formate.When i'm printing alert(data) in ajax it is showing all html page can any one suggest me how to solve this, `

<div>
    <input type="text" id="searchtext">
    <input type="button" id="verifybtn" value="search" "> 

</div>

<table class="table">
        <tr>
        <th>Application ID</th>
        <th>Application Type</th>
        <th>Customer ID</th>
        <th>Remarks<th>
        </tr>
    <tr>
            <c:forEach var="list4" items="">

            </c:forEach>
    </tr>
    </table>

this is my ajax`

$("#verifybtn").click(function(){

    var s =  $("#searchtext").val();
    //alert(s);
    $.ajax({
            url:"${pageContext.request.contextPath}/officer/search.html",
            type:"GET",
            data:{searchtext:s},
            dataType:"html",
            success:function(data){
                alert("data is"+data),
                $("result").html(data);
            }   
    })

})

`controller

@RequestMapping(value="/search")
public String search(@RequestParam("searchtext") String strtosearch,HttpSession hs,Model m)
{


    ArrayList<Application> all=(ArrayList<Application>)service.searchbyApplicationID(strtosearch);
    return "Home";
}
rikky
  • 331
  • 2
  • 7
  • 17
  • possible duplicate of http://stackoverflow.com/questions/5908466/jquery-spring-mvc-requestbody-and-json-making-it-work-together/ – Utkarsh Jul 28 '15 at 10:28

2 Answers2

0

In your controller you should return your ArrayList.

Controller:

@RequestMapping(value="/search")
@ResponseBody
public ArrayList search(@RequestParam("searchtext") String strtosearch,HttpSession hs,Model m)
{
ArrayList<Application> all=(ArrayList<Application>)service.searchbyApplicationID(strtosearch);
return all;
}

You can then access the ArrayList in your ajax as

success:function(responseData){
var entity_obj = responseData.responseJSON;
$.each(entity_obj,
    function(key, val) {
    console.log(val);
});
Anand
  • 305
  • 1
  • 2
  • 9
0

I strongly suggest you to read this detailed answer here JQuery, Spring MVC @RequestBody and JSON - making it work together

You should use annotation @ResponseBody in your controller method, so that it returns JSON string not html. Also, you need to convert your arraylist to JSON using libraries like Jackson or gson. But if you have Jackson on your classpath (and have an <mvc:annotation-driven> setup), Spring would it would serialize the returned object to JSON (if you added the @ResponseBody annotation). So the Browser / Client would see this JSON result.

@RequestMapping(value="/search")
@ResponseBody
public String search(@RequestParam("searchtext") String strtosearch,HttpSession hs,Model m)
{

    ArrayList<Application> all=(ArrayList<Application>)service.searchbyApplicationID(strtosearch);

// Convert ArrayList to JSON 

//  return jsonString;
}

Detailed tutorial to convert JAVA objects to JSON can be found here

After, getting the response you cannot directly add the json to your html page. JSON object is not html.

Community
  • 1
  • 1
Utkarsh
  • 589
  • 3
  • 19