zoukankan      html  css  js  c++  java
  • 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

    "[
     {
     "Id": 10041,
     "PageName": "01234567890",
     "MetaId": 1000,
     "TemplateId": 2
     },
     {
     "Id": 10001,
     "PageName": "about",
     "MetaId": 1000,
     "TemplateId": 1
     },
     {
     "Id": 10056,
     "PageName": "fdgdfgdfg",
     "MetaId": 1000,
     "TemplateId": 1
     },
     {
     "Id": 10052,
     "PageName": "hjkhjk",
     "MetaId": 1000,
     "TemplateId": 2
     },
     {
     "Id": 10059,
     "PageName": "jjjjjjj",
     "MetaId": 1000,
     "TemplateId": 1
     },
     {
     "Id": 10057,
     "PageName": "qqqqq",
     "MetaId": 1000,
     "TemplateId": 2
     },
     {
     "Id": 10054,
     "PageName": "qwqw",
     "MetaId": 1000,
     "TemplateId": 2
     }
    ]"
  • 相关阅读:
    牛客 158F 青蛙 (贪心)
    牛客 158D a-贝利福斯数
    长沙理工大学第十二届ACM大赛-重现赛 大家一起来数二叉树吧 (组合计数)
    美团2017年CodeM大赛-初赛B轮 黑白树 (树形dp)
    美团2017年CodeM大赛-初赛A轮 C合并回文子串
    活动安排问题
    0和5
    1489 蜥蜴和地下室
    1067 Bash游戏 V2
    1062 序列中最大的数
  • 原文地址:https://www.cnblogs.com/sdream/p/5381487.html
Copyright © 2011-2022 走看看