使用 $.getJSON 时出现问题

发布于 2024-07-26 03:53:54 字数 5297 浏览 2 评论 0 原文

我使用 YQL 调用以下 URL,

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

但我正在努力使用

$.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){

示例仅显示受限 URL 错误。

任何人都可以帮忙 - 我只是解析数据并打印到屏幕上。 提前致谢!

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":" --=+++ ","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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能否归途做我良人 2024-08-02 03:53:55

您必须将 URL 中的回调参数(当前为 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文