-1

Possible Duplicate:
How to parse JSON in JavaScript

Hi I have to parse the following response using AJAX.I have been trying it but I didnt get.

[{"Status":"True"},{"Data":[{"ApplicationNo":"RE09","ApplicationName":"Salim"},{"ApplicationNo":"RE13","ApplicationName":"V Damodaran"}]}]

 <script type="text/javascript">

     $(document).ready(function () {
         $("#btnlogin").click(function (e) {
             e.preventDefault();
              var txtUsernameID = $("input#useridtxt").val();
             var txtPasswordID = $("input#pwdtxt").val(); 
             alert(txtUsernameID+","+txtPasswordID);

             $.ajax({
                 type: 'POST',
                 //data: '{"username":"' + $("input#txtusername").val() + '","password":"' + $("input#txtpassword").val() + '"}',
                 url: '',
                 contentType: 'application/json; charset=utf-8',
                 dataType: 'json',

                 success:
              function (url, textStatus, XMLHttpRequest) {




                 $.each(url.Data, function(index, item) {
                    alert(item.agentid);

                });         



                 },
                 error:
              function (XMLHttpRequest, textStatus, errorThrown) {
                  alert(textStatus);
              }

             });

         });
     });


</script>
Community
  • 1
  • 1
user751828
  • 85
  • 4
  • 14

2 Answers2

1

You don't need to parse the JSON at all, the ajax method does that for you when you specify 'json' as data type.

What you get is an array with two items, and the Data property is in the object in the second array item, so you reach it using url[1].Data:

$.each(url[1].Data, function(index, item) {
  alert(item.agentid);
});
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

in youre success function you need to parse the response to json. most browsers have a native JSON.parse() function you can use to test with

VeXii
  • 3,079
  • 1
  • 19
  • 25