访问此JSON数据时,为什么会出现此未捕获的类型错误?

时间:2014-02-23 21:09:49

标签: jquery json

我正在从Guardian API请求数据,并收到以下错误消息:

  

未捕获的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's 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's 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"
        }
      },

1 个答案:

答案 0 :(得分:0)

我在网址'show-fields = all'中缺少一个参数。