Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help getting this Spring 3/AJAX/JSON drop down population working
    primarykey
    data
    text
    <p>I am trying to do a Spring MVC 3 ajax call to populate a drop down from json serialized data.</p> <p>The problem I am seeing is I am not sure what format of JSON Spring is returning for my list, and why.</p> <p>The method which returns the Ajax data looks like this:</p> <pre><code>@RequestMapping(value="/getMyObjects", method=RequestMethod.POST) @ResponseBody public List&lt;MyObject&gt; getMyObjects () { List&lt;MyObject&gt; myobjects = // populate list return myobjects; </code></pre> <p>}</p> <p>And as far as I understand that should be all I need to do, right? </p> <p>In my app logs I see it is indeed converting the response to JSON, as follows:</p> <p>2012-06-20 11:08:21,125 DEBUG (AbstractMessageConverterMethodProcessor.java:139) - Written [[MyObject [id=1376, name=Something test], MyObject [id=1234 name=More test]]] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@d1b7e5]</p> <p>But that JSON string looks odd to me, is that valid?</p> <p>I was expecting stuff like [{ id : 1376, name="Something test"}, { id : 1234, name="More test"}]</p> <p>On the client side when I get the response and do an alert I see it says its an array of objects like this: [Object object] [Object object] and I dont know how to deal with this data.</p> <p>I try: alert(data); -- gives the output I just described above $(data).each(function() { alert(this.id); // undefined! });</p> <p>How do I use this kind of JSON data or how do I convert it to something more manageable?</p> <p>[Edit] Attaching my client side code with current alert responses I am trying: $.ajax({ type : "POST", url : "getMyObjects", success : function(data) { alert(data); // [Object object] [Object object] alert(data.value); // Undefined $(data).each(function() { alert(this.id); // Undefined for each iteration }); }, error : function (XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); } });</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload