【问题标题】:Get score for an url on Reddit, using jQuery getJSON使用 jQuery getJSON 在 Reddit 上获取 url 的分数
【发布时间】:2012-01-30 19:34:08
【问题描述】:

我正在尝试从 reddit 获取某个 URL 的分数。添加“与 reddit 分享(计数)”链接:

function redditCounter(url) {
  // Get number of counts from reddit JSON api
  // $.getJSON('http://www.reddit.com/api/info.json?url='+url+'',
  $.getJSON('http://www.reddit.com/api/info.json?url=http://stackoverflow.com/q/811074/1288',
    function(data) {
      count = 0;
      if (data.children.count() > 0) {
        first_child = data.children[0];
        alert(first_child.score);
      }
    });
}

当您使用 curl 或您的浏览器调用 url 时,结果与 Reddits API documentation 中描述的一样。有一个 children 数组,其中包含一个带有数字的分数。

然而,$.getJSON 返回一个空答案,在 firebug 中踩到它时会看到。

这可能是reddit的一种保护方法吗?还是我使用 getJSON 错误?

【问题讨论】:

    标签: jquery getjson reddit


    【解决方案1】:

    我很确定您实际上应该期待 JSONP 响应,在这种情况下,我很确定您应该将 &callback=? 附加到您的 URL。

    编辑:对于将来偶然发现此问题的任何人,请注意:jQuery 的 getJSON() 如果遇到格式错误的 JSON 对象,或者在本例中从另一个没有 JSONP 的域接收 JSON 对象,它将静默失败填充。但是,某些浏览器(例如:Google Chrome)可能会抛出 MIME 类型警告,该警告可以很好地指示您获取的 JSON 对象有问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-05
      • 2012-01-18
      • 2018-02-09
      • 2019-02-20
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多