jQuery getJSON() 似乎无法正常工作
我所拥有的..
$.getJSON('ui-DashboardWidgetsGet.php', function(msg)
{
alert(msg);
if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
else
{
ztsDashboardJSON = msg;
}
});
var ztsDashboardJSONCount = ztsDashboardJSON.widgets[0].length;
其中转储
{ "widgets": [{ "column1": [ {"weight": 1, "bID": 1, "hideMe": false, "collapse": false, "titleOf": "Test 1", "colorOf": "color-yellow", "theFunction": "functionName"}, {"weight": 2, "bID": 2, "hideMe": false, "collapse": false, "titleOf": "Test 2", "colorOf": "color-green", "theFunction": "functionName"}, {"weight": 3, "bID": 3, "hideMe": false, "collapse": false, "titleOf": "Test 3", "colorOf": "color-blue", "theFunction": "functionName"} ], "column2": [ {"weight": 1, "bID": 4, "hideMe": false, "collapse": false, "titleOf": "Test 4", "colorOf": "color-white", "theFunction": "functionName"}, {"weight": 2, "bID": 5, "hideMe": false, "collapse": false, "titleOf": "Test 5", "colorOf": "color-red", "theFunction": "functionName"}, {"weight": 3, "bID": 6, "hideMe": false, "collapse": false, "titleOf": "Test 6", "colorOf": "color-orange", "theFunction": "functionName"} ], "column3": [ {"weight": 1, "bID": 7, "hideMe": false, "collapse": false, "titleOf": "Test 7", "colorOf": "color-white", "theFunction": "functionName"}, {"weight": 2, "bID": 8, "hideMe": false, "collapse": false, "titleOf": "Test 8", "colorOf": "color-green", "theFunction": "functionName"}, {"weight": 3, "bID": 9, "hideMe": false, "collapse": false, "titleOf": "Test 9", "colorOf": "color-blue", "theFunction": "functionName"} ] }]}
哪个是有效的 http://jsonlint.com/
它请求回显的 php从数据库中出来的,它只是回声,没有别的。然而我尝试在 JavaScript 中处理结果,但它似乎没有提供我想要的东西..
我的错误:
ztsDashboardJSON.widgets 未定义 [中断此错误] var ztsDashboardJSONCount = ztsDashboardJSON.widgets[0].length;
What I have..
$.getJSON('ui-DashboardWidgetsGet.php', function(msg)
{
alert(msg);
if(msg.error == "yes"){console.log('Error Found: '+ msg.errorMsg);}
else
{
ztsDashboardJSON = msg;
}
});
var ztsDashboardJSONCount = ztsDashboardJSON.widgets[0].length;
which dumps
{ "widgets": [{ "column1": [ {"weight": 1, "bID": 1, "hideMe": false, "collapse": false, "titleOf": "Test 1", "colorOf": "color-yellow", "theFunction": "functionName"}, {"weight": 2, "bID": 2, "hideMe": false, "collapse": false, "titleOf": "Test 2", "colorOf": "color-green", "theFunction": "functionName"}, {"weight": 3, "bID": 3, "hideMe": false, "collapse": false, "titleOf": "Test 3", "colorOf": "color-blue", "theFunction": "functionName"} ], "column2": [ {"weight": 1, "bID": 4, "hideMe": false, "collapse": false, "titleOf": "Test 4", "colorOf": "color-white", "theFunction": "functionName"}, {"weight": 2, "bID": 5, "hideMe": false, "collapse": false, "titleOf": "Test 5", "colorOf": "color-red", "theFunction": "functionName"}, {"weight": 3, "bID": 6, "hideMe": false, "collapse": false, "titleOf": "Test 6", "colorOf": "color-orange", "theFunction": "functionName"} ], "column3": [ {"weight": 1, "bID": 7, "hideMe": false, "collapse": false, "titleOf": "Test 7", "colorOf": "color-white", "theFunction": "functionName"}, {"weight": 2, "bID": 8, "hideMe": false, "collapse": false, "titleOf": "Test 8", "colorOf": "color-green", "theFunction": "functionName"}, {"weight": 3, "bID": 9, "hideMe": false, "collapse": false, "titleOf": "Test 9", "colorOf": "color-blue", "theFunction": "functionName"} ] }]}
Which is valid as per http://jsonlint.com/
It requests the php which echo's that out from a db, it echo's just that, nothing else. Yet I am trying to work with the result in the JavaScript there after and it doesn't seem to be supplying me what I want..
my error:
ztsDashboardJSON.widgets is undefined [Break On This Error] var
ztsDashboardJSONCount = ztsDashboardJSON.widgets[0].length;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要:
因为“widgets”是映射到对象数组的键。
如果您想获取列的长度,可以执行以下操作:
此处演示。
要遍历对象以使用所有小部件、列和列值,您可以执行以下操作:
此处演示。
You need:
since "widgets" is a key mapped to an array of objects.
If you're trying to get the length of a column, you can do:
Demo here.
To traverse your object to make use of all the widgets, columns and column values, you can do this:
Demo here.