Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems using $.getJSON

I'm calling the following URL using YQL

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22UTG.L%22)%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=cbfunc

this returns the following in JSON

cbfunc({"query":{"count":"1","created":"2009-06-27T11:53:44Z","lang":"en-US","updated":"2009-06-27T11:53:44Z","uri":"http://query.yahooapis.com/v1/yql?q=select+*+from+yahoo.finance.quotes+where+symbol+in+%28%22UTG.L%22%29","diagnostics":{"publiclyCallable":"true","url":[{"execution-time":"468","content":"http://datatables.org/alltables.env"},{"execution-time":"634","content":"http://www.datatables.org/yahoo/finance/yahoo.finance.quotes.xml"},{"execution-time":"351","content":"http://download.finance.yahoo.com/d/quotes.csv?s=UTG.L&f=aa2bb2b3b4cc1c3c6c8dd1d2ee1e7e8e9ghjkg1g3g4g5g6ii5j1j3j4j5j6k1k2k4k5ll1l2l3mm2m3m4m5m6m7m8nn4opp1p2p5p6qrr1r2r5r6r7ss1s7t1t7t8vv1v7ww1w4xy"},{"execution-time":"357","content":"select * from csv where url=@url and columns='Ask,AverageDailyVolume,Bid,AskRealtime,BidRealtime,BookValue,Change&PercentChange,Change,Commission,ChangeRealtime,AfterHoursChangeRealtime,DividendShare,LastTradeDate,TradeDate,EarningsShare,ErrorIndicationreturnedforsymbolchangedinvalid,EPSEstimateCurrentYear,EPSEstimateNextYear,EPSEstimateNextQuarter,DaysLow,DaysHigh,YearLow,YearHigh,HoldingsGainPercent,AnnualizedGain,HoldingsGain,HoldingsGainPercentRealtime,HoldingsGainRealtime,MoreInfo,OrderBookRealtime,MarketCapitalization,MarketCapRealtime,EBITDA,ChangeFromYearLow,PercentChangeFromYearLow,LastTradeRealtimeWithTime,ChangePercentRealtime,ChangeFromYearHigh,PercebtChangeFromYearHigh,LastTradeWithTime,LastTradePriceOnly,HighLimit,LowLimit,DaysRange,DaysRangeRealtime,FiftydayMovingAverage,TwoHundreddayMovingAverage,ChangeFromTwoHundreddayMovingAverage,PercentChangeFromTwoHundreddayMovingAverage,ChangeFromFiftydayMovingAverage,PercentChangeFromFiftydayMovingAverage,Name,Notes,Open,PreviousClose,PricePaid,ChangeinPercent,PriceSales,PriceBook,ExDividendDate,PERatio,DividendPayDate,PERatioRealtime,PEGRatio,PriceEPSEstimateCurrentYear,PriceEPSEstimateNextYear,Symbol,SharesOwned,ShortRatio,LastTradeTime,TickerTrend,OneyrTargetPrice,Volume,HoldingsValue,HoldingsValueRealtime,YearRange,DaysValueChange,DaysValueChangeRealtime,StockExchange,DividendYield'"}],"javascript":{"instructions-used":"66024"},"user-time":"1537","service-time":"1810","build-version":"1949"},"results":{"quote":{"symbol":"UTG.L","Ask":"129.00","AverageDailyVolume":"354500","Bid":"128.50","AskRealtime":"129.00","BidRealtime":"128.50","BookValue":"0.00","Change_PercentChange":"+7.50 - +6.19%","Change":"+7.50","Commission":null,"ChangeRealtime":"+7.50","AfterHoursChangeRealtime":"N/A - N/A","DividendShare":"0.00","LastTradeDate":"6/26/2009","TradeDate":null,"EarningsShare":"0.00","ErrorIndicationreturnedforsymbolchangedinvalid":"N/A","EPSEstimateCurrentYear":"0.00","EPSEstimateNextYear":"0.00","EPSEstimateNextQuarter":"0.00","DaysLow":"122.00","DaysHigh":"128.75","YearLow":"36.25","YearHigh":"319.75","HoldingsGainPercent":"- - -","AnnualizedGain":"-","HoldingsGain":null,"HoldingsGainPercentRealtime":"N/A - N/A","HoldingsGainRealtime":null,"MoreInfo":"cred","OrderBookRealtime":"N/A","MarketCapitalization":null,"MarketCapRealtime":null,"EBITDA":"0","ChangeFromYearLow":"+92.50","PercentChangeFromYearLow":"+255.17%","LastTradeRealtimeWithTime":"N/A - <b>128.75<\/b>","ChangePercentRealtime":"N/A - +6.19%","ChangeFromYearHigh":"-191.00","PercebtChangeFromYearHigh":"-59.73%","LastTradeWithTime":"Jun 26 - <b>128.75<\/b>","LastTradePriceOnly":"128.75","HighLimit":null,"LowLimit":null,"DaysRange":"122.00 - 128.75","DaysRangeRealtime":"N/A - N/A","FiftydayMovingAverage":"125.714","TwoHundreddayMovingAverage":"100.63","ChangeFromTwoHundreddayMovingAverage":"+28.12","PercentChangeFromTwoHundreddayMovingAverage":"+27.94%","ChangeFromFiftydayMovingAverage":"+3.036","PercentChangeFromFiftydayMovingAverage":"+2.42%","Name":"UNITE GROUP","Notes":"-","Open":"122.00","PreviousClose":"121.25","PricePaid":null,"ChangeinPercent":"+6.19%","PriceSales":null,"PriceBook":null,"ExDividendDate":"14-Apr-04","PERatio":null,"DividendPayDate":"13-May-05","PERatioRealtime":null,"PEGRatio":null,"PriceEPSEstimateCurrentYear":null,"PriceEPSEstimateNextYear":null,"Symbol":"UTG.L","SharesOwned":null,"ShortRatio":null,"LastTradeTime":"11:35am","TickerTrend":"&nbsp;--=+++&nbsp;","OneyrTargetPrice":null,"Volume":"254927","HoldingsValue":null,"HoldingsValueRealtime":null,"YearRange":"36.25 - 319.75","DaysValueChange":"- - +6.19%","DaysValueChangeRealtime":"N/A - N/A","StockExchange":"London","DividendYield":null,"PercentChange":"+6.19%"}}}});

But I'm struggling to use the data (I'm a newbie with jQuery and JSON) using

$.getJSON("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22UTG.L%22)%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=cbfunc",
    function(data){

from this example just flashes a restricted URL error.

Anybody help - I simply was to parse the data and print to screen. Thanks in advance!

like image 877
toomanyairmiles Avatar asked Jun 27 '09 12:06

toomanyairmiles


People also ask

Is getJSON JavaScript?

jQuery getJSON() MethodjQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous for its philosophy of “Write less, do more”. The getJSON() method in jQuery fetches JSON-encoded data from the server using a GET HTTP request.

What is the difference between getJSON and Ajax in jQuery?

getJSON() is equal to $. ajax() with dataType set to "json", which means that if something different than JSON is returned, you end up with a parse error. So you were mostly right about the two being pretty much the same :).

Is getJSON an Ajax call?

getJSON()) is an AJAX method that is used to fetch JSON data using HTTP GET request.

How to get JSON format data in jQuery?

To load JSON data using jQuery, use the getJSON() and ajax() method. The jQuery. getJSON( ) method loads JSON data from the server using a GET HTTP request. data − This optional parameter represents key/value pairs that will be sent to the server.


1 Answers

You'll have to replace your callback parameter (currently cbfunc) in the URL with ?. jQuery will automatically replace the ? with a correct callback.

$.getJSON(
  "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22UTG.L%22)%0A%09%09&format=json&env=http%3A%2F%2Fdatatables.org%2Falltables.env&callback=?", // the ? at the end is the key
  function(data) {
    // and now do anything you want to with the data (this is just an example)
    var html = []; // we're using an array and join for IE performance reasons
    data = data.query;
    for (var key in data) {
      html.push([key, ' = ', data[key]].join(''));
    }
    $('#debug').html(html.join('<br />'));
  }
);

Update: Here's a demo: http://jsbin.com/owebo

like image 96
moff Avatar answered Nov 08 '22 17:11

moff