我想通过javascript访问我的公共要点,但以下代码不起作用:
$(document).ready(function() {
var url = 'https://api.github.com/users/binroot/gists';
$.getJSON(url, function(data) {
document.write(data[0].description);
});
});
怎么了?
这可能是一个同源策略问题。GitHub API 支持 JSONP,因此您可以使用它。jQuery会获取您的URL中的callback=?
,并将自动使用JSONP。
$.getJSON('https://api.github.com/users/binroot/gists?callback=?', function(data) {
// do whatever as before, but note that your data
// will now be in a property called "data" with the
// header information in "meta"
});
由于 Access-Control-Allow-Origin 的原因,您无法使用 JavaScript 从不同的来源请求资源,有关 gist API 的更多信息,请查看此:http://developer.github.com/v3/gists/