0

I'm trying to parse YAHOO data using getJSON and YQL

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:

http://jsbin.com/umuri5/1/edit

with little success. Here is my JSP:

<%@page import="p.build.classes.p.*"%>

<!DOCTYPE JSP>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<title>untitled</title>
<style type="text/css">
    body { text-align: center; }
</style>
</head>
<body onLoad="gova();">

<div id="container">

</div>
<table id="userdata" border="1">
    <thead>
        <th>Change</th>
    </thead>
    <tbody></tbody>
</table>

<script id="userTemplate" type="text/x-jquery-tmpl">
    <tr>
        <td>${Change}</td>
    </tr>
</script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"    type="text/javascript" charset="utf-8"></script>   
<script src="https://raw.github.com/jquery/jquery-tmpl/master/jquery.tmpl.min.js" type="text/javascript" charset="utf-8"></script> 
<script src="scripts/script.js"></script>
<script type="text/javascript">
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 < results.userdata.length; i++) {
            t.tmpl(results.userdata[i]).appendTo(tbody);
        }
    }
);
return false;
}
</script>
</body>
</html>

and here is my script, script.js:

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)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&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 && 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);


 };

The error occurs at the line:

        for (var i = 0; i < results.userdata.length; i++) {
            t.tmpl(results.userdata[i]).appendTo(tbody);
        }

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.

Thank you in advance.

0

1 Answer 1

1

The results portion of the response from the URL you're making the request to returns:

results: {
     quote: {
          Change: "-0.061"
     }
}

Are you expecting this?

If so, you'll want to grab data.query.results['quote']['Change'] or data.query.results.quote.Change instead of trying to pass data.query.results.json into the callback and then look through results.userdata

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for the help, but when I replace the loop that returns the error with for (var i = 0; i < results.quote.Change.length; i++) { t.tmpl(results.quote.Change[i]).appendTo(tbody); } I get the same error, but with "quote" undefined instead.
there's no need to loop through anything. simply grab the response: data.query.results.quote.Changeand pass that into tmpl. You'll want to keep in mind that it's worth checking to see if each sub-object exists as you're accessing them, but that'll get you going.
I'm sorry, there still appears to be something wrong with data.query.results.quote.Change. After removing the loop and simply referring to t.tmpl(data.query.results.quote.Change).appendTo(tbody); I cannot get access to the data returned, it says, in this case, that data is not supported by tmpl.
I've also changed the data.query.results.json callback in my script
wanna create a fiddle? can certainly help debug that
|

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.