JSON AJAX未定义项

时间:2012-12-21 14:06:17

标签: ajax jsonp

我遇到了通过AJAX显示JSON的问题。

Feed没有显示 - 浏览器显示

  

未捕获的TypeError:无法读取未定义的属性“title”

我想我在这些方面有问题:

$.each(data, function(i, item) {
            var news = '<h1>' + item.posts.title + '</h1>' 
                + '<p>' + item.posts.content + '</p>' 
                + '<p>' + item.posts.custom_fields.ecpt_chill + '</p>';

See the full code in action at JSFiddle

2 个答案:

答案 0 :(得分:1)

您获得的错误意味着typeof item.postsundefined;例如它没有设定任何价值。

要测试您从服务器返回的内容,请将以下行放在$.each(data, …之前:

console.log(JSON.stringify(data));

然后,您可以查看Web服务器返回的JSON。

顺便说一句,您已将请求类型设置为POST。在这种情况下,正确的方法是GET

返回以下JSON:

{"status":"ok","count":1,"count_total":1,"pages":1,"posts":[{"id":587,"type":"weather","slug":"conditions-for-magnitogorsk-ru-at-329-am-yekt","url":"http://www.domain.com/weather/conditions-for-magnitogorsk-ru-at-329-am-yekt/","status":"publish","title":"Conditions for Magnitogorsk, RU at 3:29 am YEKT","title_plain":"Conditions for Magnitogorsk, RU at 3:29 am YEKT","content":"<p><img src=\"http://l.yimg.com/a/i/us/we/52/20.gif\" /><br />\n<b>Current Conditions:</b><br />\nFog, -28 C<BR /><br />\n<BR /><b>Forecast:</b><BR /><br />\nWed &#8211; Partly Cloudy. High: -15 Low: -26<br />\nThu &#8211; Mostly Sunny. High: -20 Low: -24</p>\n<p><a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Magnitogorsk__RU/*http://weather.yahoo.com/forecast/RSXX0058_c.html\">Full Forecast at Yahoo! Weather</a><BR /><BR /><br />\n(provided by <a href=\"http://www.weather.com\">The Weather Channel</a>)</p>\n","excerpt":"Current Conditions: Fog, -28 C Forecast: Wed &#8211; Partly Cloudy. High: -15 Low: -26 Thu &#8211; Mostly Sunny. High: -20 Low: -24 Full Forecast at Yahoo! Weather (provided by The Weather Channel)","date":"2012-12-11 22:29:00","modified":"2012-12-21 14:06:27","categories":[],"tags":[],"author":{"id":1,"url":"","description":""},"comments":[],"attachments":[],"comment_count":0,"comment_status":"closed","custom_fields":{"ecpt_chill":["eqweqwe"]}}]}

格式化:

{
  "status": "ok",
  "count": 1,
  "count_total": 1,
  "pages": 1,
  "posts": [{
      "id": 587,
      "type": "weather",
      "slug": "conditions-for-magnitogorsk-ru-at-329-am-yekt",
      "url": "http://www.cityfacts.pro/weather/conditions-for-magnitogorsk-ru-at-329-am-yekt/",
      "status": "publish",
      "title": "Conditions for Magnitogorsk, RU at 3:29 am YEKT",
      "title_plain": "Conditions for Magnitogorsk, RU at 3:29 am YEKT",
      "content": "<p><img src=\"http://l.yimg.com/a/i/us/we/52/20.gif\" /><br />\n<b>Current Conditions:</b><br />\nFog, -28 C<BR /><br />\n<BR /><b>Forecast:</b><BR /><br />\nWed &#8211; Partly Cloudy. High: -15 Low: -26<br />\nThu &#8211; Mostly Sunny. High: -20 Low: -24</p>\n<p><a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Magnitogorsk__RU/*http://weather.yahoo.com/forecast/RSXX0058_c.html\">Full Forecast at Yahoo! Weather</a><BR /><BR /><br />\n(provided by <a href=\"http://www.weather.com\">The Weather Channel</a>)</p>\n",
      "excerpt": "Current Conditions: Fog, -28 C Forecast: Wed &#8211; Partly Cloudy. High: -15 Low: -26 Thu &#8211; Mostly Sunny. High: -20 Low: -24 Full Forecast at Yahoo! Weather (provided by The Weather Channel)",
      "date": "2012-12-11 22:29:00",
      "modified": "2012-12-21 14:06:27",
      "categories": [],
      "tags": [],
      "author": {
          "id": 1,
          "url": "",
          "description": ""
      },
      "comments": [],
      "attachments": [],
      "comment_count": 0,
      "comment_status": "closed",
      "custom_fields": {
          "ecpt_chill": ["eqweqwe"]
      }
  }]
}

查看代码,您将循环返回返回的JSON的每个键。第一个键是status,当然,它没有posts属性。

如果您想循环返回的帖子,您应该执行以下操作:

$.each(data.posts, function(i, item) {

并调整您的代码以使用它。

答案 1 :(得分:0)

您需要检查您尝试加载的内容。未定义表示您未正确加载对象。尝试使用Firefox的Poster扩展名来检查源内容。