0

I have a Jquery AJAX POST method.Which call a web method of asp.net. The ajax method get a json data. The data format is :

[
    {
        "Title": "Test2",
        "Name" : "AMIT",
        "IsRoot": "True"
    },
    {
        "Title": "Test3",
        "Name" : "AMIT1",
        "IsRoot": "False"
    },
    {
        "Title": "Test4",
        "Name" : "AMIT2",
        "IsRoot": "True"
    }
]

I validate the dataformat in "http://jsonlint.com/" site and it's telling that dataformat is correct. I want to loop through the data and access each of the attribute.But I am not able to get that. I try to find the total array length which should be 3.But it's giving me 9(means each attribute ) I try

alert(data.d.length); // giving 9 (should give 3)

var jsondata = data.d;

alert(jsondata[1].Title); //undefined (should give Test3)
alert(jsondata[2].Title); //undefined (should give Test4)
alert(jsondata[1].Name); //undreined (should give AMIT1)

var key, count = 0;
for (key in data.d) {
    if (data.d.hasOwnProperty(key)) {
        count++;
    }
}
alert(count); // giving 9 (should give 3)

any help is highly accepted.

My ajax calling method is

 $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "WebForm1.aspx/GetRootData",
    dataType: "json",
    success: function (data, textStatus) {
        var jsondata = data.d;

        alert(jsondata[1].Title);
        alert(jsondata[2].Title);
        alert(jsondata[1].MimeType);

        var key, count = 0;
        for (key in data.d) {
            if (data.d.hasOwnProperty(key)) {
                count++;
            }
        }
        alert(count);
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(xhr.responseText);
        alert(thrownError);
    }
});

and don't know why debugger also not working.. :(

amit ghosh
  • 276
  • 1
  • 4
  • 15
  • 4
    `alert` is not a useful debugging tool. Use the debugger built into your browser to set breakpoints, single-step through the code, inspect variables, etc. All modern browsers have them. In Chrome or IE, for instance, just press F12 to open the debugger. – T.J. Crowder Jul 20 '13 at 14:02
  • From the symptoms you've described, you're not getting the data you expect. If you use the debugger as mentioned above, you can see what the real data you're getting back is. – T.J. Crowder Jul 20 '13 at 14:03
  • add your Ajax calling code also – Govind Malviya Jul 20 '13 at 14:07
  • Here is a tutorial about how to use the console: http://www.netmagazine.com/tutorials/javascript-debugging-beginners. Also have a look at this question: http://stackoverflow.com/q/11922383/218196. What is `data`? Why are you accessing `data.d`? – Felix Kling Jul 20 '13 at 14:15
  • added the Ajax calling code.. @Govind KamalaPrakash Malviya – amit ghosh Jul 20 '13 at 14:19
  • Do a `console.log(data);` in the `success` handler and post the result you get here. – Hieu Nguyen Jul 20 '13 at 14:33

1 Answers1

0

Thanks everyone.Finally I solved the problem.

the solution is

var jsonObject = eval(data.d);

Now it's returning all the data fine..

alert(jsonObject.length); //Now it's returning 2

alert(jsonObject[1].Title); // it's returning Test3 now.

Thanks..

Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
amit ghosh
  • 276
  • 1
  • 4
  • 15
  • 2
    That's sounds like you have double encoded your data somehow. Don't use `eval` in this case! Either parse the JSON with `$.parseJSON` or *better*, fix your server side code to not double encode your data. – Felix Kling Jul 20 '13 at 15:00
  • Thanks for your suggestion. $.parseJSON also working fine..Up-voted the suggestion.. – amit ghosh Jul 20 '13 at 15:47