【问题标题】:JSON AJAX undefined itemJSON AJAX 未定义项
【发布时间】:2012-12-21 23:18:35
【问题描述】:

我在通过 AJAX 显示 JSON 时遇到问题。

Feed 未显示 - 浏览器显示

未捕获的类型错误:无法读取未定义的属性“标题”

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

$.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

【问题讨论】:

  • 这里的服务器端技术是什么? .Net,还有别的吗?我们需要看到实际返回的数据。
  • 仅供参考,在你的小提琴中,这个 `alert(item);` 提醒“ok”...所以你有一些服务器端需要解决。
  • 嗨,马克,我正在使用 wordpress (PHP) 将数据加载到数据库中。如果您可以尝试复制和粘贴 json 提要本身 - 它在浏览器中可以正常工作...

标签: ajax jsonp


【解决方案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) {

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

【讨论】:

  • 您好 user2428118,非常感谢。一旦我完成更改,它就会显示更多错误....
  • 我查找了jQuery.each 的语法并更新了我的答案。
  • 这很简单。非常感谢user2428118!你为我节省了很多时间,我的朋友。
  • 这里是工作代码:jsfiddle.net/u2GQL/1。顺便说一句,由于您是直接将返回的数据添加到代码中,因此如果返回的数据中包含&lt;&gt; 等特殊字符,则可能会遇到麻烦。
  • 您可以使用$('&lt;ELEMENT&gt;') 创建每个元素,然后在添加它们之前添加如下文本:$('&lt;ELEMENT&gt;').text(item.SOMETHING)。例如,output.append($('&lt;h1&gt;').text(item.title))。但是,这不适用于item.content,因为它已经是 HTML。
【解决方案2】:

您需要检查您尝试加载的内容。未定义意味着您没有正确加载对象。尝试使用 Firefox 的 Poster 扩展程序检查源内容。

【讨论】:

    猜你喜欢
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    相关资源
    最近更新 更多