2016-03-25 5 views
0

Чтобы сделать его простым, ajax получает ответ jsonify от фляги, и я не могу найти способ его рендерить в html. Я пробовал:jsonify fls parsing error

success: function(json) { 
    $.each(json, function(index, element) { 
     $('#main').append(element.title); 
    }); 
} 

и получил "неопределенными" напечатано.

Тогда я попробовал:

$('#main').append(json[index].name) 

но ничего не отпечатались

Вот JSON, возвращаемый API:

{ 
    "albums": [ 
    { 
     "artist_id": 19, 
     "composer_id": 3, 
     "id": 6, 
     "img": "The_Fine_Art_of_Self_Destruction.jpg", 
     "release_date": 2002, 
     "title": "the fine art of self destruction" 
    }, 
    { 
     "artist_id": 26, 
     "composer_id": 6, 
     "id": 22, 
     "img": "The_Blasters_%28album%29.jpg", 
     "release_date": 1981, 
     "title": "the blasters" 
    }, 
    { 
     "artist_id": 25, 
     "composer_id": 6, 
     "id": 25, 
     "img": "XMoreFunInTheNewWorld.jpg", 
     "release_date": 1983, 
     "title": "more fun in the new world" 
    }, 
    { 
     "artist_id": 27, 
     "composer_id": 8, 
     "id": 28, 
     "img": "220px-The_Angels_of_Light_Sing_%27Other_People%27.jpeg", 
     "release_date": 2005, 
     "title": "the angels of light sing 'other people'" 
    }, 
    { 
     "artist_id": 10, 
     "composer_id": 10, 
     "id": 32, 
     "img": null, 
     "release_date": 2008, 
     "title": "when the flood comes" 
    }, 
    { 
     "artist_id": 31, 
     "composer_id": 15, 
     "id": 40, 
     "img": "The_Willies.jpg", 
     "release_date": 2002, 
     "title": "the willies" 
    } 
    ] 
} 

я смотрел на многие вещи, как jQuery.parseJSON() и другие, но ничего не получилось.

Благодарим за помощь!

+0

'JSON [индекс] .name' должен быть' json.albums [индекс] .name' –

ответ

0
$.each(json, function(index, element) { 

должен быть

$.each(json.albums, function(index, element) { 
0

Обновите свой код

$.each(json.albums, function(index, element) { 
     $('#main').append(element.title); 
    });