getJson 并解析以创建列表(目前) jQuery/Yahoo Finance API

发布于 2024-12-20 08:48:38 字数 851 浏览 1 评论 0原文

我几乎可以正常工作了,但我无法解决解析问题。如果有人可以帮助我将非常感激!

我正在尝试查询 Yahoo Finance API 并使用 jQuery 解析结果。这是我执行此操作的代码:

  <script>
$(document).ready(function(){

var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'NPO'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback";

$.getJSON(url + "&format=json&jsoncallback=?", function(data) {
  var items = [];

  $.each(data, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
});


});
</script>

但我收到此错误: 在此处输入图像描述

任何克服此错误的帮助将不胜感激。

谢谢!

I almost have the thing working but I can't get past a parsing issue. If anyone can help I would be very thankful!

I am trying to query Yahoo Finance API and parse the results using jQuery. Here is my code to do so:

  <script>
$(document).ready(function(){

var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'NPO'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback";

$.getJSON(url + "&format=json&jsoncallback=?", function(data) {
  var items = [];

  $.each(data, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
});


});
</script>

But I am getting this error:
enter image description here

Any help overcoming this error would be much appreciated.

Thanks!

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

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

发布评论

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

评论(3

下雨或天晴 2024-12-27 08:48:38
  • 服务未使用 jsoncallback 参数。
  • 您甚至不必指定回调参数。它是通过 getJSON() 添加的
  • format 参数已在 url 中指定
  • 您的 items 数组将对象存储为数据位于 data.query.results.quote

试试这个:

var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'NPO'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

$.getJSON(url, function(data) {
  var items = [];
  $.each(data.query.results.quote, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });
  $('<ul/>', { 'class': 'my-new-list', html: items.join('')}).appendTo('body');
});

工作代码是 这里

  • jsoncallback parameter is not used by the service.
  • You even don't have to specify callback parameter. It is added by the getJSON()
  • format parameter is already specified in url
  • Your items array is storing objects as the data are under data.query.results.quote

Try this:

var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'NPO'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

$.getJSON(url, function(data) {
  var items = [];
  $.each(data.query.results.quote, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });
  $('<ul/>', { 'class': 'my-new-list', html: items.join('')}).appendTo('body');
});

Working code is HERE.

怀里藏娇 2024-12-27 08:48:38

YQL 使用 callback=? 参数,而不是 jsoncallback=? 尝试以下操作:

var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'NPO'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

$.getJSON(url + "&format=json&callback=?", function(data) {

编辑:注意,url 也必须更改。

YQL uses a callback=? parameter, not jsoncallback=? try this:

var url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'NPO'&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

$.getJSON(url + "&format=json&callback=?", function(data) {

Edit: Note, the url had to change too.

不弃不离 2024-12-27 08:48:38

当您删除 &jsoncallback=? 时,对我来说效果很好。

$(document).ready(function() {

    var url = "http://query.yahooapis.com/v1/public/yql?" +                    
              "q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'NPO'" + 
              "&format=json&diagnostics=true" + 
              "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

    $.getJSON(url, function(data) {

        console.log( data );

    });
});

实际上,主字符串中已经有了 format=json

JSFIDDLE 演示

Works fine for me when you remove the &jsoncallback=?.

$(document).ready(function() {

    var url = "http://query.yahooapis.com/v1/public/yql?" +                    
              "q=select%20*%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D'NPO'" + 
              "&format=json&diagnostics=true" + 
              "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

    $.getJSON(url, function(data) {

        console.log( data );

    });
});

You actually already have the format=json in the main string.

JSFIDDLE DEMO

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