0

ETA: Here's the JSFiddle, check your console: http://jsfiddle.net/GZNwK/1/

So I'm just trying to load a subreddit from the IMGUR API:

$.getJSON('http://imgur.com/r/cats.json?callback=?',function(data){
    console.log(data)
})

But I end up with this error:

enter image description here

Not exactly sure why this is. I can load the Flickr API and the Instagram API in .jsom format, the syntax appears to be exactly the same. Why is the IMGUR API giving me an error? Also, if I remove ?callback=? then it it doesn't use JSONP and the cross-domain request fails.

alt
  • 13,357
  • 19
  • 80
  • 120
  • You simply linked us jsfiddle.net. You have to save your fiddle before you can copy and paste the link. I do that mistake a lot, too... – Kiruse Aug 12 '12 at 23:49

1 Answers1

3

Because this is not a resource that supports JSONP requests. I don't know a whole lot about IMGUR, but it looks like they do have an API you can use: http://api.imgur.com/

Another solution would be to use YQL:

$.ajax({
    url: 'http://query.yahooapis.com/v1/public/yql',
    data: {
        q: 'select * from json where url="http://imgur.com/r/cats.json"',
        format: 'json'
    },
    type: 'get',
    dataType: 'jsonp'
}).success(function (data) {
    /* results are in data.query.results.json.gallery */
    alert(data.query.results.json.gallery[0].title);
});

Example: http://jsfiddle.net/sNSEA/

Although I would strongly recommend doing this the "right" way through the API if possible (a quick glance didn't yield anything).

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • @JacksonGariety, You will have to proxy it over from your server. Since JSONP isn't available, there is no way to bypass the restrictions and get it client-side. – Brad Aug 12 '12 at 23:48
  • @JacksonGariety: You can also use YQL. One second and I'll get an example working – Andrew Whitaker Aug 12 '12 at 23:49
  • I thought I was using their API. Their docs dont say anything about grabbign subreddits. How could I use their API to grab imgur.com/r/cats page? – alt Aug 12 '12 at 23:53
  • I don't think you're using their API. I think that's just a resource on their site that happens to serve up JSON. – Andrew Whitaker Aug 12 '12 at 23:56
  • Oh, thanks. But your YQL thing worked! Unfortunately, for some reason, all the "source" items are null! Any idea why the source values are lost. – alt Aug 13 '12 at 00:18
  • If you navigate to the JSON resource in your browser you'll notice that there are many null values for the `source` property... YQL isn't changing them to null. – Andrew Whitaker Aug 13 '12 at 00:20