我是制作处理其他 API 的应用程序的新手,尤其是那些需要 OAuth 身份验证的应用程序。
目前,我只是尝试在我的应用程序中获取有关 Reddit 首页列表的信息。
我正在查看这个 Reddit API 文档 here ,但后来我读到要获得 Reddit 的 JSON 表示形式,您只需在 url 后添加一个 .json。
所以我发送了一个 HTTP GET 请求,例如:
$(document).ready(function () {
httpGetAsync("https://www.reddit.com/.json");
});
function httpGetAsync(url) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
alert(JSON.stringify(xmlHttp.responseText, null, 4));
}
xmlHttp.open("GET", url, true); // true for asynchronous
xmlHttp.send(null);
}
但这似乎只返回了 reddit 页面上的最后一篇帖子,或者我可能无法判断,因为警告框无法显示巨大的 JSON 响应?
我认为是这种情况并尝试过:
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
parseResponse(xmlHttp.responseText);
}
xmlHttp.open("GET", url, true); // true for asynchronous
xmlHttp.send(null);
function parseResponse(responseText) {
var x = (JSON.parse(responseText));
alert(x.count);
}
但在警告框中出现未定义。有任何想法吗?
目标是获取reddit首页25的JSON响应信息(标识符)
最佳答案
您可能想使用 jQuery 来检索和解析数据:
$.getJSON( "https://www.reddit.com/.json", function( data ) {
$.each( data.data.children, function( i, obj ) {
console.log(obj.data.id);
});
});
我为您制作了一个检索前 25 个 ID 的工作示例:
关于javascript - Reddit API - API 与附加 .json 和获取首页信息之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34280219/