这是我的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
发布评论