Click here to Skip to main content
15,849,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to read the json values in ajax jquery . i am getting values like below from serialized json format


[{"pkMenuId":68,"Submenu":"Book Online"},{"pkMenuId":69,"Submenu":"E-Brochure"},{"pkMenuId":70,"Submenu":"MemberShips"}]


The above values need to bind in div as li tag.. when i try to read the values from json i am getting error like undefined, here my code.

XML
<script type="text/javascript">
       function showmsg(val) {
           alert("Test12");
           $.ajax({
               cache: false,
               async: true,
               type: "GET",
               url: "../AdminServices.svc/GetSubmenu",
               data: { PmID: val },
               dataType: "JSON",
               contentType: "application/json;charset=utf-8",
               success: function (data) {
                   $.each(data, function (i, item) {
                       $('#ShowSubMenu').html('');
                       $('#ShowSubMenu').append('<li><a href="yourlink?id=' + item.pkMenuId + '">' + item.Submenu + '</a></li>');
                   });

               },
               error: function (e) { alert(e.statusText); }
           });
       }
   </script>
Posted

JavaScript
$.ajax({async:true, dataType:"json", url:"page.aspx", type:"GET"}).done(function(res) { 
            for(i = 0; i < res.Table.length; i++) {
                tblHtml += '<table><tbody><tr class="rowwhitesmoke" dirid="+res.Table[i].data1+" id="trHand" onclick="fundata(this);">';
                tblHtml += '<td align="center">'+res.Table[i].data2+'</td>';
                tblHtml += '</tr></tbody></table>';
            }
            tblHtml += '';
            dvgrd.html(tblHtml);
        });
 
Share this answer
 
The data that you are receiving is an object array. You can access them like this:
data[0].pkMenuId
data[0].Submenu
data[1].pkMenuId
... and so on.

In short:
[] = array and {} = object so [{}, {},...] is an object array. This example array is empty by the way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900