gpt4 book ai didi

jquery - 使用 jQuery getJSON 在 Reddit 上获取 url 的分数

转载 作者:行者123 更新时间:2023-12-01 00:11:27 24 4
gpt4 key购买 nike

我正在尝试从 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中描述的那样。有一个子数组,其中包含带有数字的分数。

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

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

最佳答案

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

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

关于jquery - 使用 jQuery getJSON 在 Reddit 上获取 url 的分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8683851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com