【发布时间】:2015-04-16 16:48:09
【问题描述】:
我已经使用这段代码从 GitHub API 获取数据
var name;
var description;
var html_url;
var username = "PirateStef";
var updated_at;
var language;
var repo;
var urlGitHub = 'https://api.github.com/users/'+username+'/repos?sort=created';
$.getJSON(urlGitHub, function(json){
repositories = json;
outputGitHubContent(); // GitHub Content
});
function outputGitHubContent() {
$.each(repositories, function(index){
name = "<div class='name'>" + repositories[index].name + "</div>";
description = "<div class='description'>" + repositories[index].description + "</div>";
updated_at = "<div class='updated_at'>" + repositories[index].updated_at.substring(0,10) + "</div>";
html_url = "<a class='html_url' target='_blank' href='" + repositories[index].html_url + "'>";
language = "<div class='language'>" + repositories[index].language + "</div>";
repo = "<div class='repo'>" + html_url + "<div>" + name + language + "</div>" + description + updated_at + "</a> </div>";
console.log(repositories[index].owner.login);
$("#github").append(repo);
});
};
这是 Github API 网址
https://api.github.com/users/PirateStef/repos?sort=updated
我尝试构建一个函数来获取运球数据。使用下面的 api.dribbble 网址。
http://api.dribbble.com/players/PirateStef/shots/
我无法从每个对象中获取“标题”。
无效的运球代码
var urlDribbble = 'http://api.dribbble.com/players/'+username+'/shots/';
var shot;
$.getJSON(urlDribbble, function(json){
shots = json;
outputDribbbleContent(); // Dribbble Content
});
function outputDribbbleContent() {
$.each(shots, function(index){
console.log(shots[index].title);
});
};
让我 5 倍未定义
console.log(shots[index]);
给我这个
[
Object
,
Object
,
Object
,
Object
,
Object
,
Object
,
Object
,
Object
,
Object
,
Object
,
Object
,
Object
,
Object
]
【问题讨论】:
-
什么是
dribble.com?它看起来像一个未使用的登录页面。为什么您希望为一个网站设计的 API 调用可以在另一个网站上运行? -
如果 API 没有启用 CORS 或 jsonp,则并非所有 API 都可以通过 ajax 获得...查看 API 文档。还有一些需要在请求中授权
-
不是绝对的,但可能是的。阅读 API 文档
-
“我无法从每个对象中获取“标题””是什么意思?您是否收到响应,但无法解析它?如果是这样,响应是什么样的? API 请求是否完全失败?您是否看到任何错误消息?
-
@user3199999,所以您收到了回复。您的代码可能有问题。请向我们展示 不 工作的 Dribbble 代码,而不是显示有效的 GitHub 代码。