2

I would like to access my public gists through javascript, but the following code doesn't work:

$(document).ready(function() {

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

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

What's wrong?

BinRoot
  • 694
  • 2
  • 8
  • 21

2 Answers2

2

This is probably a same-origin policy problem. The GitHub API supports JSONP, so you can use that. jQuery picks up on callback=? in your URL and will automatically use 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"
});
Community
  • 1
  • 1
John Flatness
  • 32,469
  • 5
  • 79
  • 81
  • John, would a request to https://gist.github.com/raw/3849235/5fb40674a675c550d251d4d8f1bb73cd9375617f/thinkpad.quote also require a callback? – BinRoot Oct 09 '12 at 03:18
0

You can not request resource from different origin with JavaScript because of the Access-Control-Allow-Origin, for more about gist api, please checkout this:http://developer.github.com/v3/gists/

jiguang
  • 352
  • 1
  • 2
  • 7