jquery loop on Json data using $.each
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]"
Answer:
var data = [
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "jjjjjjj"}
];
$.each(data, function(i, item) {
alert(data[i].PageName);
});
$.each(data, function(i, item) {
alert(item.PageName);
});
these two options work well, unless you have something like:
var data.result = [
{"Id": 10004, "PageName": "club"},
{"Id": 10040, "PageName": "qaz"},
{"Id": 10059, "PageName": "jjjjjjj"}
];
$.each(data.result, function(i, item) {
alert(data.result[i].PageName);
});
EDIT:
try with this and describes what the result
$.get('/Cms/GetPages/123', function(data) {
alert(data);
});
FOR EDIT 3:
this corrects the problem, but not the idea to use "eval", you should see how are the response in '/Cms/GetPages/123'.
$.get('/Cms/GetPages/123', function(data) {
$.each(eval(data.replace(/[\r\n]/, "")), function(i, item) {
alert(item.PageName);
});
});
jquery loop on Json data using $.each的更多相关文章
- jQuery loop over JSON字符串 – $.each实例
先来一段简单的javascript对象的遍历: var json = [ {"id":"1","tagName":"apple&q ...
- jQuery.parseJSON vs JSON.parse
转载:http://stackoverflow.com/questions/10362277/jquery-parsejson-vs-json-parse 一.JavaScript函数JSON.par ...
- javascript: Jquery each loop with json array or object
http://www.codeproject.com/Articles/779303/JSON-and-Microsoft-technologies http://www.codeproject.co ...
- jQuery解析JSON出现SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data 我在使用$.parseJSON解析后 ...
- jquery 如何遍历json
var obj = {"status":1,"bkmsg":"\u6210\u529f","bkdata":[" ...
- JQuery ajax返回JSON时的处理方式
最近在使用JQuery的ajax方法时,要求返回的数据为json数据,在处理的过程中遇到下面的几个问题,那就是采用不同的方式来生成json数据的时候,在$.ajax方法中应该是如何来处理的,下面依次来 ...
- 关于Jquery中ajax方法data参数用法的总结
data 发送到服务器的数据.将自动转换为请求字符串格式.GET 请求中将附加在 URL 后.查看 processData 选项说明以禁止此自动转换.必须为 Key/Value 格式.如果为数组,jQ ...
- jQuery 中使用 JSON
转载:http://www.cnblogs.com/haogj/archive/2011/12/01/2271098.html JSON 格式 json 是 Ajax 中使用频率最高的数据格式,在浏览 ...
- jquery ajax返回json数据进行前后台交互实例
jquery ajax返回json数据进行前后台交互实例 利用jquery中的ajax提交数据然后由网站后台来根据我们提交的数据返回json格式的数据,下面我来演示一个实例. 先我们看演示代码 代码如 ...
随机推荐
- solr 时区问题
本人使用solr版本5.0.0,使用jetty启动 solr默认UTC时区,与我们相差八小时,按照网络上资料修改 C:\Users\hp\Desktop\solr-5.0.0\bin 下的solr.i ...
- 负载均衡之Ocelot+Consul(配置文件注册服务)
继上篇 Ocellot 做负载均衡之后,本篇将记录 Ocelot + Consul 试验如何做服务发现和服务注册. 服务发现和服务注册的背景知识,一搜满街都是. 在此,我还是写下自己对这个术语的理解吧 ...
- asp.net静态变量研究
asp.net的webform,请求一个页面,如index.aspx,每一次都会交给不同的线程来处理. 经过个人测试,不管是页面类的静态属性,还是工具类的静态属性,都不会因为session的过期而改变 ...
- POJ 2777 Count Color【线段树】
题目大意:要求完成以下两个操作:1.将一个区间刷上一种颜色2.询问一段区间上有多少种颜色 思路:这两个操作线段树都可以很迅速的完成,具体做法是:线段树上每个节点存这个线段上的颜色数量,由于颜色数很少, ...
- Asp.Net Thread is being Aborted
Asp.Net做的一个程序,通过JQuery的Ajax调用,程序执行的数据时间比较长,程序部署到服务器后执行一段时间后就弹出执行失败的对话框,日志记录的错误信息是“正在中止线程”. 查错过程: 1.根 ...
- 【2018 Multi-University Training Contest 5】
01: 02:https://www.cnblogs.com/myx12345/p/9436953.html 03: 04: 05:https://www.cnblogs.com/myx12345/p ...
- 【Codeforces Round #501 (Div. 3)】
A:https://www.cnblogs.com/myx12345/p/9842904.html B:https://www.cnblogs.com/myx12345/p/9842964.html ...
- HDU 6076 (动态规划)
HDU 6076 Security Check Problem : 有两个长度为n的队列过安检,每个人有一个特征值.如果两个队列中的第一个人的特征值之差小于等于k,那么一次只能检查其中一个人,否则一次 ...
- PDO的基本操作
PDO的基本操作 连接到mysql: try { $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); foreach($ ...
- loj6165 一道水题(线性筛)
题目: https://loj.ac/problem/6165 分析: 最直接的想法就是把1~n的所有数分解质因数,然后每个素数的幂取max 我们首先来看看一共可能有哪些素数? 实际上这些素因数恰好就 ...