Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript each loop over JSON only getting first element?
    primarykey
    data
    text
    <p>I'm using Jquery mobile, so ignore some of the following overdone css, its not related to the core issue.</p> <p>I'm having a problem looping over the "Places" in my JSON packet/javascript object. I am getting a response with multiple "Places", but can't seem to figure out how to iterate over them. My 'i' variable in my each loop is working correctly for the first element, and displaying its corresponding name &amp; image.</p> <p>Here's my server side Django view (pretty straight-forward if your unfamiliar with Python):</p> <pre><code>def tonight_mobile(request): callback = request.GET.get('callback', '') def with_rank(rank, place): return (rank &gt; 0) place_data = dict( Places = [make_mobile_place_dict(request, p) for p in Place.objects.all()] ) xml_bytes = json.dumps(place_data) return HttpResponse(xml_bytes, content_type='application/json; charset=utf-8') </code></pre> <p>My server is acknowledging the request and returning the following:</p> <pre><code>"GET /api/0.1/tonight-mobile.json&amp;callback=jsonp1293142434434 HTTP/1.1" 200 167 </code></pre> <p>Here's my response:</p> <pre><code>callback({"Places": [{"url": "http://localhost:8000/api/0.1/places/3.plist", "image_url": "http://localhost:8000/static/place_logos/Bengals_1.png", "name": "Bob's Place", "events": 2}, {"url": "http://localhost:8000/api/0.1/places/2.plist", "image_url": "http://localhost:8000/static/place_logos/Makonde_Mask.gif", "name": "My Bar", "events": 0}, {"url": "http://localhost:8000/api/0.1/places/1.plist", "image_url": "http://localhost:8000/static/place_logos/Quintons_1.png", "name": "Quinton's", "events": 1}]}) </code></pre> <h2>------------</h2> <p><em><strong>My getJSON and callback method have evolved into this:</em></strong></p> <pre><code>&lt;script&gt; function loadJSON(){ $.getJSON("http://localhost:8000/api/0.1/tonight-mobile.json&amp;callback=?", callback(data)); } function callback(data){ $("#tonight-list").each(data.Places, function(i) { $(this).append("&lt;li role='option' tabindex='" + data.Places[i] + "' class='ui-li-has-thumb ui-li ui-btn ui-btn-icon-right ui-corner-top ui-corner-bottom ui-controlgroup-last ui-btn-down-c ui-btn-up-c' data-theme='c'&gt;&lt;div class='ui-btn-inner ui-corner-top ui-corner-bottom ui-controlgroup-last'&gt;&lt;span class='ui-icon ui-icon-arrow-r'&gt;&lt;/span&gt;&lt;div class='ui-btn-text'&gt;&lt;img src=" + data.Places[i].image_url + " alt=" + data.Places[i].name + " class='ui-li-thumb ui-corner-tl ui-corner-bl'/&gt;&lt;h1 class='list-title ui-li-heading' id='list-title'&gt;&lt;a href='detail.html?id=slide' data-transition='slide' class='ui-link-inherit'&gt;" + data.Places[i].name + "&lt;/a&gt;&lt;/h1&gt;&lt;span class='ui-li-count ui-btn-up-c ui-btn-corner-all'&gt;" + data.Places[i].events + " events&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;"); }); } &lt;/script&gt; </code></pre> <p>Am I confusing how the each function iterates over the JSON (which become) Javascript objects? I am pretty sure the each is my issue here, as I am only getting the FIRST element of the "Places" list. Can someone please help me out as to how to structure the looping?</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.
 

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