Search code examples
javascriptjqueryjsonimgur

Outputting imgur image URLs from a gallery using the API using javascript


I am trying to write a script that gets a random image from an imgur album, but I cannot retrieve the image URLs.

I am using AJAX to get the JSON data for the album, here is what I have so far:

$.ajax({
    type: "GET",
    url: "https://api.imgur.com/3/album/Cfy6A/images",
    dataType: "text",
    beforeSend: function(xhr) {
        xhr.setRequestHeader('Authorization', 'Client-ID <client-id>');
    },
    success: function(data) {
        var json = $.parseJSON(data);
        console.log(json);
    }
});

I am able to get the JSON data, and I am trying to get 'link' for each object, I just cannot get it to output just the links as an array.

Any help is appreciated!

EDIT: Here is a pastebin of the JSON response: http://pastebin.com/JR068Dhf


Solution

  • You can use a map function to get all the links into an array

    var links = json.data.map(function(item) {
        return item.link;
    })