0

I'm having some problems with JSON.Parse in my code and I cant find the reason of this I have a function which call two ajax functions, one on the start and another on the success function . Its working fine but when I'm try to parse the second one response the code breaks without giving any error and the real mystery is JSON.parse(object); dosent give any problem but when I use a variable to store the result like this var list =JSON.parse(object); my code broke and I dont what is the reason behind this my current code is given below

function getData()
{
            $.ajax({
                type: "POST",
                url: "MyPage.aspx/GetData",
                data: JSON.stringify({ data: data})
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    var result = JSON.parse(response.d);
                    var temp = 0;
                    for (var i = 0; i < result.length; i++) {

                        if (result[i].data > 1) {
                            var subList = JSON.parsegetFullData (result[i].id));
                        }

                }
            });
}
     function getFullData (id) {
            var sublist;
            $.ajax({
                type: "POST",
                url: "MyPage.aspx/GetData2",
                data: JSON.stringify({ id: id }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    return response.d;
                }
            });

        }

any thought will help a lot

Optimus
  • 2,200
  • 8
  • 37
  • 71
  • Are you sure `response.d` is a string containing JSON? Btw, `return response.d;` from the callback won't have any effect. – Felix Kling Oct 03 '13 at 05:33
  • Then it wouldn't throw an error. Please post the response. – Felix Kling Oct 03 '13 at 05:37
  • @FelixKling thats what I said its not throwing any error – Optimus Oct 03 '13 at 05:42
  • This is the response "[{"id":0,"LastName":test0,"FirstName":test0,"Age":10,"DOB":"1/23/2013"},{{"id":1,"LastName":test1,"FirstName":test1,"Age":11,"DOB":"2/23/2013"}},{{"id":2,"LastName":test2,"FirstName":test2,"Age":12,"DOB":"3/23/2013"}},{{"id":3,"LastName":test3,"FirstName":test3,"Age":13,"DOB":"4/23/2013"}}]" – Optimus Oct 03 '13 at 05:43
  • 1
    Ok, those `{{` are not valid. If any, this is an array of objects, not an object with property `d`. And in your title you are saying `JSON.Parse causes error in javascript`. – Felix Kling Oct 03 '13 at 05:44
  • @FelixKling sorry The extra } is my typing error I've to change the data – Optimus Oct 03 '13 at 05:46
  • "[{"id":0,"LastName":test0,"FirstName":test0,"Age":10,"DOB":"1/23/2013"},{"id":1,"LastName":test1,"FirstName":test1,"Age":11,"DOB":"2/23/2013"},{"id":2,"LastName":test2,"FirstName":test2,"Age":12,"DOB":"3/23/2013"},{"id":3,"LastName":test3,"FirstName":test3,"Age":13,"DOB":"1/23/2013"}]" – Optimus Oct 03 '13 at 05:47
  • The above is the actual response format I got from The server – Optimus Oct 03 '13 at 05:47
  • 1
    Ok, so `response` will be an array of objects. And arrays don't have a property `d`. Just do `result = response;` or rename the function parameter. I wonder why thought you have to use `response.d`? – Felix Kling Oct 03 '13 at 05:50
  • @FelixKling its not working when i return the the value its shows undefined – Optimus Oct 03 '13 at 05:53
  • You cannot return the value from the callback function. See [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Felix Kling Oct 03 '13 at 05:54

1 Answers1

2

When you use $.ajax with dataType:"json", the response is already parsed for you. And there doesn't seem to be a reason to try to parse response.d.

Simply use

$.ajax({
           type: "POST",
           url: "MyPage.aspx/GetData",
           data: JSON.stringify({ data: data})
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (results) {
                for (var i = 0; i < results.length; i++) {
                    console.log(results[i].id, results[i].LastName);
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I know that the server response is a serialized collection and when I inspect response.d its a large string – Optimus Oct 03 '13 at 05:34
  • Is it a JSON string ? Why would you put JSON into JSON ? – Denys Séguret Oct 03 '13 at 05:35
  • I want it as json object not string and also the method JSON.parse(data) works perfectly in the first function – Optimus Oct 03 '13 at 05:37
  • 1
    @Optimus: [There is no such thing as a "JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). – Felix Kling Oct 03 '13 at 05:39
  • Typically, an object, even deeply structured, is serialized into a sole JSON string. You only have to parse this string to get the whole structure, you don't have to parse the internal properties. – Denys Séguret Oct 03 '13 at 05:41
  • @Optimus I've seen your JSON in comment, there is no reason to parse the properties. Directly use the response which is an array. I've added an example of use of the properties (the `console.log`). – Denys Séguret Oct 03 '13 at 05:52
  • its not working when i return the the value its shows undefined – Optimus Oct 03 '13 at 05:54