如何使用JSON从Reddit API提取url数据
How to extract url data from Reddit API using JSON
我正试图从subreddit feed中提取图像帖子url,并在我的页面上呈现<img>
元素。
一直试图破解.getJSON()
Flickr的例子从jQuery文档现在一段时间,我没有得到任何地方。
问题代码:
$.getJSON('http://www.reddit.com/r/pics.json', function (data) {
$.each(data.children, function (i, item) {
$('<img/>').attr("src", url).appendTo("#images");
});
});
在正文中,我有元素:div#images
我知道我需要使用JSONP,但不确定如何使用。有人能给我指个正确的方向吗?
您使用了错误的url。使用这个:
$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) {
// Do whatever you want with it..
});
编辑:基于您的小提琴在评论中的工作示例。
$.getJSON("http://www.reddit.com/r/pics/.json?jsonp=?", function(data) {
$.each(data.data.children, function(i,item){
$("<img/>").attr("src", item.data.url).appendTo("#images");
});
});
您应该使用data.data.children
而不是data.children
网上迷路的朋友:
fetch("http://www.reddit.com/r/pics/.json")
.then(r => r.json()).then((r) => {
r.data.children.forEach((i) => {
try{
document.body.appendChild(Object.assign(document.createElement("img"),{src: i.data.thumbnail}))
} catch (error){console.log(error.message)}
})})
相关文章:
- Regex提取URL返回数组的一部分;未定义”;
- 通过正则表达式从字符串中提取Url
- 如何提取URL并将其存储到MySQL中
- 如何使用JQuery提取URL中最后一个/分隔符之前的最后一个值
- 如何使用哈希提取URL参数
- 使用 JavaScript 提取 URL 参数
- Javascript:从字符串(包括查询字符串)中提取URL并返回数组
- 提取 URL 的中间部分并将其附加到新的外部链接以在 IE8 中显示
- regexp(?) 用于从另一个 URL 中提取 URL
- 使用正则表达式提取 url 路径
- 从 iFrame 中提取 url 变量
- 从字符串 javascript 中提取 URL
- 如何使用 jquery 提取 URL 参数
- 在JavaScript中使用正则表达式提取URL的特定部分
- 在Node.js中从服务器提取URL
- 主干提取url数据格式错误
- 提取 URL 的最后一个字符串
- Regex从文本中提取URL(有/没有协议和www或子域)
- 使得网络爬虫想要提取url中的图像
- 从给定字符串中提取 URL