使用$ .getJSON时出现问题

时间:2009-06-27 12:57:11

标签: javascript jquery json jsonp

我正在使用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

这将在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%"}}}});

但我正在努力使用数据(我是jQuery和JSON的新手)

$.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){
来自this示例的

只会闪现受限制的网址错误。

任何人的帮助 - 我只是解析数据并打印到屏幕。提前谢谢!

1 个答案:

答案 0 :(得分:14)

您必须使用cbfunc替换网址中的回调参数(目前为?)。 jQuery会自动用正确的回调替换?

$.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 />'));
  }
);

更新:这是一个演示:http://jsbin.com/owebo