0

i want to access in my jquery script to the variable cardlist i put in the model:

@RequestMapping(value="/searchlostcard") 
public  
String searchlostcard(@ModelAttribute(value="searchform") SearchForm searchForm
        ,HttpServletRequest request, Map<String, Object> model) {   


    List<Lostcard> listlostcard = lostcardRepository.findByNom(searchForm.getValue());

    model.put("cardlist", listlostcard);

    return "search/results";

}

My jquery ajax call:

function searchAjax() {
$.ajax({
    url : 'searchlostcard',
    type: 'POST',
    data:$('#formSearch').serialize(),
    success : function(responce) {  

        /* how can i acces to my List of object (listlostcard) ? */
        $('#page_grid').html(responce);
    },      
    error : function(){
        alert("error");
    }
});

}

10
  • You need to understand that Java is a server side component in this case, while javascript is a client side component. The List no longer exists on the client side. Commented Jan 8, 2014 at 18:56
  • but the variable it send with request. so we can get it from the client side. Commented Jan 8, 2014 at 18:59
  • The server response with an HTTP response which contains HTML content with nested javascript. The browser (the client) can execute the javascript. There is no concept of java object or variables at this point. Commented Jan 8, 2014 at 19:00
  • take a look at this topic i try it but i return me something wrong. Commented Jan 8, 2014 at 19:02
  • 1
    You aren't accessing the variable from javascript. The JSP is rendering the ${something} expression into some String. The rendered text (which is simply HTML) is sent as the body of the HTTP response. Your browser receives that body, sees something like var javascriptVar = "someText";, evaluates it and executes it. It's just text. Commented Jan 8, 2014 at 19:12

2 Answers 2

1

Change you method as:

@RequestMapping(value="/searchlostcard") 
@ResponseBody
public  
List<Lostcard> searchlostcard(@ModelAttribute(value="searchform") SearchForm searchForm
        ,HttpServletRequest request) {   


    List<Lostcard> listlostcard = lostcardRepository.findByNom(searchForm.getValue());

    return listlostcard ;

}

make sure you have Jackson Mapper in the classpath. Then you can access the list in the success mehtod.

To use the response in the you can use:

success : function(responce) {  

    jQuery.each(response, function(i, val) {
                    alert(val.someProperty);
                });
}
Sign up to request clarification or add additional context in comments.

4 Comments

And how is $('#page_grid').html(responce);supposed to work then?
It's not. I said the list can be accessed in the success method. You need to iterate over it...
So your solution isn't working or at least not complete. For the JavaScript part is still the same.
I updated the answer with Jquery example. pur JS can also be used. Instead of alert the result can be appended to the html. someProperty can be any Lostcard property.
0

use Gson api to create a json string and put it in your model. to use Gson add this to your pom.xml:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>1.7.1</version>
</dependency>

then do this in your java code:

List<Lostcard> listlostcard = lostcardRepository.findByNom(searchForm.getValue());
Gson gson = new GsonBuilder().create();
String json = gson.toJson(listlostcard);
model.put("cardlist", json);
return "search/results";

then read it in your js file like this:

success : function(responce) {  

    var cardlist = JSON.parse(responce);
    var tbl = document.createElement("table");
    for(var i=0;i<cardlist.length;i++){
        var row = tbl.insertRow(i);
        //let's say you have ID and title in your model
        var idCell = row.insertCell(0);
        idCell.innerHTML = cardlist[i]["ID"];
        var titlecell = row.insertCell(1);
        titlecell.innerHTML = cardlist[i]["title"];
    }
    $('#page_grid').append(tbl);
}

10 Comments

responce would still be HTML
yes var cardlist = JSON.parse(responce); responce is html !
no it is a json string if you want to convert it to html, first you have to decide how would you like to display your data, for instance in a table or a list...
So if if you know it doesn't work, why do you include it in your code?
I put it there just to print the json data as an string in the page. but if you want I can add another part to make it like a table.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.