Я запрашиваю данные из API Guardian и получаю следующее сообщение об ошибке:
Uncaught TypeError: Невозможно прочитать "миниатюру" свойства неопределенного
Я знаю, что поля являются свойством элемента в массиве результатов (я только включил первый результат для краткости) и что он также является объектом со своими собственными свойствами, но я не уверен, почему поля не идентифицируются.
Я получаю доступ к данным с помощью метода jQuery Ajax.
JQuery
$.ajax({
url: "http://content.guardianapis.com/football/liverpool?show-factboxes=all&edition=UK&show-most-viewed=true&date-id=date%2Flast7days",
data: {
// enter your developer api key here
apikey: "hidden",
// the type of data you're expecting back from the api
_accept: "application/json"
},
dataType: "jsonp",
success: function(data) {
for (var i = 0; i < data.response.results.length; i++) {
var link = data.response.results[i].webUrl;
var image = data.response.results[i].fields.thumbnail;
var head = data.response.results[i].fields.headline;
$('body').append('<a href="' + link + '"><h1>' + head + '</h1></a>');
$('body').append('<img src="' + image + '"">');
}
},
error: function() {
console.log('error');
}
});
Данные.
JSON
{
"response": {
"status": "ok",
"userTier": "free",
"total": 15,
"startIndex": 1,
"pageSize": 10,
"currentPage": 1,
"pages": 2,
"orderBy": "newest",
"tag": {
"id": "football/liverpool",
"type": "keyword",
"webTitle": "Liverpool",
"webUrl": "http://www.theguardian.com/football/liverpool",
"apiUrl": "http://content.guardianapis.com/football/liverpool",
"sectionId": "football",
"sectionName": "Football"
},
"results": [
{
"id": "football/gallery/2014/feb/23/premier-league-liverpool-newcastle",
"sectionId": "football",
"sectionName": "Football",
"webPublicationDate": "2014-02-23T16:34:00Z",
"webTitle": "Premier League: Sunday matches – in pictures",
"webUrl": "http://www.theguardian.com/football/gallery/2014/feb/23/premier-league-liverpool-newcastle",
"apiUrl": "http://content.guardianapis.com/football/gallery/2014/feb/23/premier-league-liverpool-newcastle",
"fields": {
"trailText": "<p>The best images from a seven-goal thriller at Anfield between Liverpool and Swansea and a less exciting one-goal game at St James Park featuring Newcastle and Aston Villa</p>",
"headline": "Premier League: Sunday matches – in pictures",
"showInRelatedContent": "true",
"lastModified": "2014-02-23T17:53:39Z",
"hasStoryPackage": "false",
"score": "1.0",
"standfirst": "The best images from a seven-goal thriller at Anfield between Liverpool and Swansea and a less exciting one-goal game at St James Park featuring Newcastle and Aston Villa",
"shortUrl": "http://gu.com/p/3n265",
"thumbnail": "http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2014/2/23/1393170159591/Liverpools-Henderson-004.jpg",
"commentable": "false",
"allowUgc": "false",
"isPremoderated": "false",
"byline": "Jonny Weeks",
"publication": "theguardian.com",
"shouldHideAdverts": "false",
"liveBloggingNow": "false",
"commentCloseDate": "2014-02-26T16:34:34Z"
}
},
Мне не хватало параметра в URL 'show-fields = all'.