Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing a JSON object (list of object) through Javascript and loop in it?
    primarykey
    data
    text
    <p>Ok, I know there's something wrong but I can't understand what.</p> <p>I read an ArrayList of Service (in JSON mediatype) from a web service that I wrote. Going to that address returns me the json string.</p> <p>Now I'm trying to make a web page for showing that values and seeing the changes making to the page a request every 3 secs.</p> <p>How can I parse it, or use it? Read lot and more and I'm still to the start..</p> <p>[Is not possible to pass this object to the JSP and parse, loop and everything with JSTL? That would be awesome!]</p> <p>Here the js code:</p> <pre><code> &lt;script type="text/javascript"&gt; setInterval(function(){ $.ajax({ url: "/MyApp/rest/display", success: function(data){ var objs = $.parseJSON(data); $.each(objs, function(i,service) { $("#service").append('&lt;p&gt;'+service+'&lt;/p&gt;'); }); }, dataType: "json"}); }, 3000); &lt;/script&gt; </code></pre> <p>I've a <code>&lt;div id="service"&gt;</code></p> <p><strong>EDIT:</strong> Almost there!</p> <p>Now I've this:</p> <pre><code>&lt;script type="text/javascript"&gt; setInterval(function(){ $.ajax({ url: "/myApp/rest/display", success: function(data){ $.each(data, function(i,service) { var cont = 1; var newdiv = document.createElement('div'); newdiv.setAttribute('id', "service"+i); $("#service"+i).html('&lt;p&gt;'+service.serviceId+" "+service.queue.lastNumber+'&lt;/p&gt;'); document.getElementById("services").appendChild(newdiv); cont++; }); }, dataType: "json"}); }, 5000); &lt;/script&gt; </code></pre> <p>It gets the updates and all (nice!) but I've one problem: <em>it keeps creating new divs</em> inside the bigger one (empty divs). How can I avoid this?</p> <p><strong>EDIT2:</strong></p> <p>Nevermind, I've done! Just add this line before the appendChild:</p> <pre><code>if(!$("#service"+i).length) </code></pre> <p>Works like a charm. Thanks!</p>
    singulars
    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