为什么$ .each说这个JSON对象是未定义的?(Why does $.each say this JSON object is undefined?)

编程入门 行业动态 更新时间:2024-10-24 04:40:59
为什么$ .each说这个JSON对象是未定义的?(Why does $.each say this JSON object is undefined?)

这是我的Javascript:

$.post('foo.php', { request: Request }, function(data) { $.each(data.chats, function(i, chat) { ... }); });

这里是JSON,通过Firebug和其他一切验证,此代码正在接收:

{ "chats": [ { "chat_id": "22", "user_status": "Listening", "user_ping": "2010-06-22 15:53:57", "messages": [ { "chat_id": "22", "line_id": "5", "message": "Hello", "timestamp": "15:53" } ] } ] }

...为什么当我运行它时,它不起作用,并且Firebug抛出了“对象未定义”的错误,并在jquery.js的第552行中给出了“length = object.length”的链接,并显示'未定义'当我做一个console.log(data.chats)? console.log(data)显示完整的JSON响应,但出于某种原因,它不想处理data.chats ...我已经缩小了'$ .each'这一行的错误范围。

我相当肯定这是一个非常简单的错误,当有人向我指出这一点时,我会面临一些问题,但在这一点上我完全困惑。 (是的,“聊天”应该是一个JSON数组,用于处理将来的多个聊天,与消息相同。)

Here is my Javascript:

$.post('foo.php', { request: Request }, function(data) { $.each(data.chats, function(i, chat) { ... }); });

And here is the JSON that, verified by Firebug and everything else, this code is receiving:

{ "chats": [ { "chat_id": "22", "user_status": "Listening", "user_ping": "2010-06-22 15:53:57", "messages": [ { "chat_id": "22", "line_id": "5", "message": "Hello", "timestamp": "15:53" } ] } ] }

... so why, when I run this, does it not work, and Firebug throws the error that the "object is undefined", with a link to "length = object.length" on line 552 of jquery.js, and shows 'undefined' when I do a console.log(data.chats)? A console.log(data) shows the full JSON response, but for some reason it doesn't want to deal with data.chats... I've narrowed the error down the '$.each' line.

I'm reasonably sure this is a very simple mistake and I'll facepalm when someone points it out to me, but at this point I'm completely confused. (and yes, "chats" is supposed to be a JSON array, to handle multiple chats in the future, same with messages.)

最满意答案

告诉它,你期待JSON - 默认情况下它会将响应视为html。

$.post('foo.php', { request: Request }, function(data) { $.each(data.chats, function(i, chat) { ... }); }, 'json');

Tell it that you are expecting JSON -- by default it will treat the response as html.

$.post('foo.php', { request: Request }, function(data) { $.each(data.chats, function(i, chat) { ... }); }, 'json');

更多推荐

chats,JSON,data,Firebug,电脑培训,计算机培训,IT培训"/> <meta name="descr

本文发布于:2023-08-05 09:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1431217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未定义   JSON   undefined   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!