Note that there are some explanatory texts on larger screens.

plurals
  1. PODifficulty parsing JSON into HTML, results undefined?
    primarykey
    data
    text
    <p>I'm trying to parse YAHOO data using getJSON and YQL</p> <p>The connection works well, I retrieve data which I can see and log to console, but I cannot then get the data to print into the JSP page I'm using. I've used the answer accepted here: </p> <p><a href="http://jsbin.com/umuri5/1/edit" rel="nofollow">http://jsbin.com/umuri5/1/edit</a></p> <p>with little success. Here is my JSP: </p> <pre><code>&lt;%@page import="p.build.classes.p.*"%&gt; &lt;!DOCTYPE JSP&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt; &lt;title&gt;untitled&lt;/title&gt; &lt;style type="text/css"&gt; body { text-align: center; } &lt;/style&gt; &lt;/head&gt; &lt;body onLoad="gova();"&gt; &lt;div id="container"&gt; &lt;/div&gt; &lt;table id="userdata" border="1"&gt; &lt;thead&gt; &lt;th&gt;Change&lt;/th&gt; &lt;/thead&gt; &lt;tbody&gt;&lt;/tbody&gt; &lt;/table&gt; &lt;script id="userTemplate" type="text/x-jquery-tmpl"&gt; &lt;tr&gt; &lt;td&gt;${Change}&lt;/td&gt; &lt;/tr&gt; &lt;/script&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt; &lt;script src="https://raw.github.com/jquery/jquery-tmpl/master/jquery.tmpl.min.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt; &lt;script src="scripts/script.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function gova() { requestCrossDomainJSON( 'yahoo.finance.quote', function(results) { var i, t = $('#userTemplate'), tbody = $('#userdata tbody'); // console.log(data.query.results); for (var i = 0; i &lt; results.userdata.length; i++) { t.tmpl(results.userdata[i]).appendTo(tbody); } } ); return false; } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>and here is my script, script.js:</p> <pre><code>function requestCrossDomainJSON( site, callback ) { if ( !site ) { alert('No site was passed.'); return false; } var yql = 'http://query.yahooapis.com/v1/public/yql?q=select%20Change%20from%20' + site + '%20where%20symbol%20in%20(%22YHOO%22)&amp;format=json&amp;diagnostics=true&amp;env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&amp;callback=?'; // Request that YSQL string, and run a callback function. // Pass a defined function to prevent cache-busting. function cbfunc(data) { console.log(data); // If we have something to work with... if ( ! data.error &amp;&amp; data.query.results) { // If the user passed a callback, and it // is a function, call it, and send through the data var. if ( typeof callback === 'function') { callback(data.query.results.json); } } // Else, Maybe we requested a site that doesn't exist, and nothing returned. else throw new Error('Nothing returned from getJSON.'); } // Request that YQL string, and run a callback function. // Pass a defined function to prevent cache-busting. $.getJSON( yql, cbfunc ); // console.log(data.query.results); }; </code></pre> <p>The error occurs at the line:</p> <pre><code> for (var i = 0; i &lt; results.userdata.length; i++) { t.tmpl(results.userdata[i]).appendTo(tbody); } </code></pre> <p>where the console says it cannot read property userdata of undefined. Because JQuery template is depreciated, should I be using a better method to get this data into my page?If this is still a valid solution to the problem I am having, how should I reference results from the called JSON if it is undefined in the function provided. I've tried using an HTML file in lieu of a JSP as well.</p> <p>Thank you in advance.</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.
    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