2

我想通过 javascript 访问我的公共要点,但以下代码不起作用:

$(document).ready(function() {

    var url = 'https://api.github.com/users/binroot/gists';

    $.getJSON(url, function(data) {
        document.write(data[0].description);                                           
    });
});

怎么了?

4

2 回答 2

2

这大概是同源政策问题。GitHub API支持 JSONP,所以你可以使用它。jQuerycallback=?在您的 URL 中获取并自动使用 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"
});
于 2012-10-09T02:26:29.463 回答
0

由于Access-Control-Allow-Origin ,您不能使用 JavaScript 从不同来源请求资源,有关 gist api 的更多信息,请查看:http: //developer.github.com/v3/gists/

于 2012-10-09T02:36:44.280 回答