博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jquery loop on Json data using $.each
阅读量:5093 次
发布时间:2019-06-13

本文共 2876 字,大约阅读时间需要 9 分钟。

Hi I have the following JSON returned in a variable called data.

THIS IS THE JSON THAT GETS RETURNED...

[ {
"Id": 10004, "PageName": "club"}, { "Id": 10040, "PageName": "qaz"}, { "Id": 10059, "PageName": "jjjjjjj"} ]

and I am trying to loop through the collection using $.each but I am running into problems where the alert is showing undefined. I have tried alot of different syntax but can't seem to figure this out.

The JQuery I am using is

$.each(data, function(i, item) { alert(item.PageName); });

Can any one point me in the right direction?

EDIT This is the code I am using to grab the data

$.getJSON('/Cms/GetPages/123', null, function(data) { fillSelect(data); });

and this is the function that gets called upon call back

function fillSelect(data) { alert(data); $.each(data, function(i, item) { alert(item.PageName); }); }

EDIT 2 This is slightly confusing me, according to the docs it should work as I have it, but it doesn't. According to fiddler the header shows:-

Content-Type: application/json; charset=utf-8

and the JSON is exactly correct above. I am using chrome if this makes any different. Will test in IE and FF....

EDIT 3

using $.get produces

"[\r\n {\r\n \"Id\": 10041,\r\n \"PageName\": \"01234567890\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10001,\r\n \"PageName\": \"about\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 1\r\n },\r\n {\r\n \"Id\": 10056,\r\n \"PageName\": \"fdgdfgdfg\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 1\r\n },\r\n {\r\n \"Id\": 10052,\r\n \"PageName\": \"hjkhjk\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10059,\r\n \"PageName\": \"jjjjjjj\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 1\r\n },\r\n {\r\n \"Id\": 10057,\r\n \"PageName\": \"qqqqq\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n },\r\n {\r\n \"Id\": 10054,\r\n \"PageName\": \"qwqw\",\r\n \"MetaId\": 1000,\r\n \"TemplateId\": 2\r\n }\r\n]"

转载于:https://www.cnblogs.com/sdream/p/5381487.html

你可能感兴趣的文章
汇编实验九
查看>>
理解熵公式
查看>>
asp.net 框架初接触
查看>>
XPTable 单元格怎么取值
查看>>
Poj 2186 Popular Cows
查看>>
文字向上滚动代码,带段落停顿
查看>>
关于取消的默认的Enter的keydown事件的疑问与解决
查看>>
云计算之路-阿里云上:重启 manager 节点引发 docker swarm 集群宕机
查看>>
上周热点回顾(9.13-9.19)
查看>>
[leetcode] integer break DP算法
查看>>
Altium designer知识总结
查看>>
Windows 7 Shell 命令大名单
查看>>
shell wc -l
查看>>
C#集合之不变的集合
查看>>
前端本质
查看>>
第一章 android系统移植与驱动开发概述
查看>>
php使用curl下载指定大小的文件
查看>>
Task 6.2冲刺会议九 /2015-5-22
查看>>
eclipse实现JavaWeb应用增量打包
查看>>
Java Web之Servlet入门篇(二)
查看>>