Retrieve specific part of a javascript object in an AJAX function

Asked

Viewed 45 times

-3

I need to retrieve an object attribute by AJAX in a $.each(), and pass it in src of an img tag.

AJAX function:

$.ajax({
        type: 'GET',
        url: `user/album/recommended/`,
        context: 'json',
        success: function(data){
            data = JSON.parse(data); 
            console.log(data);
            var cont = 1;
            $.each(data, function(index, value){
                $("#album"+cont+"").append('<img src="'+value.data.cover_medium+'"/>');
                cont++;
            });
            $(".new-album").show();
        },
        error: function(){
            console.log('Erro no AJAX de recomendação');
        },
    });

console.log(data) returns this:

1 answer

-2


You are making $.each incorrectly

$.each(data.data, function(index, value){
   $("#album"+cont+"").append('<img src="'+value.cover_medium+'"/>');
   cont++;
});
  • Could you explain to me the reason for the negative?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.