1

The following ajax call gives the following result:

    $.ajax({
        type: "POST",
        url:  //**My full URL goes here**,
        data: {sources: sources},
        dataType: "json",
        success: function(data) {
            alert(data);
            alert(data.length);
            for (var i = 0; i < data.length; i++)
            {
                alert(data[i]);
            }
        }
    });

Result:

data:
[objject object],[objject object],[objject object]

length:
3

in loop:
[objject object]
[objject object]
[objject object]

and the following code , in which I just added:

var data = $.parseJSON(data);
$.ajax({
    type: "POST",
    url:  //**My full URL goes here**,
    data: {sources: sources},
    dataType: "json",
    success: function(data) {
        var data = $.parseJSON(data);
        alert(data);
        alert(data.length);
        for (var i = 0; i < data.length; i++)
        {
            alert(data[i]);
        }
    }
});

The above code gives me the following error:

Uncaught Syntax Error: Unexpected token o

Why is that? Am I doing something wrong? How can I fix it?

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 3
    You are already receiving a json object. Then why are you parsing that again.? – Rajaprabhu Aravindasamy Feb 12 '14 at 03:59
  • 2
    The first code is correct, what's the problem? jQuery already parsed the JSON for you and now you have an array of objects. You are converting the objects to strings, and that's why you see `[object Object]`. **Everything is OK**. If you want to know how to access the data properly: [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/q/11922383/218196). Regarding the error in your second code: That's because `$.parseJSON` expects a *string*, not an array or object. – Felix Kling Feb 12 '14 at 04:00
  • Thanks :) in loop I want to access the content of object! How can I do that ? –  Feb 12 '14 at 04:00
  • Yes you already received parsed data – Jain Feb 12 '14 at 04:00
  • 1
    `alert()` is not a very good debugging tool. See [`console.log`](https://developer.mozilla.org/en-US/docs/Web/API/console.log) – Phil Feb 12 '14 at 04:01
  • You might use `console.log(data[1])` rather than `alert(data[1])` in either Chrome developer tools or Firefox's firebug. Hit f12 in chrome to open up the developer tools... – scrappedcola Feb 12 '14 at 04:02

2 Answers2

3

The data is already a parsed object since you are passing dataType: 'json', so there is no need to parse it again.

Again to debug and inspect the value of data, use console logging instead of alert(), like console.log(data)

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • Thanks Arun! So could you please let me know how I can access the content of my object in my first solution instead of having [objject object] –  Feb 12 '14 at 04:01
  • probably `data[0]` or something – Tim Vermaelen Feb 12 '14 at 04:02
  • from what you have shared it looks like `data` is an array... can you do the `console.log(data)` and see what is the structure of the data... as an alternate you can also try `console.log(JSON.stringify(data))` to print a string representation – Arun P Johny Feb 12 '14 at 04:03
  • @user3000457: That depends on which properties the objects have. Please have a look at http://stackoverflow.com/q/11922383/218196. I also recommend to read the [MDN documentation about objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects). – Felix Kling Feb 12 '14 at 04:03
  • @user3000457 also you can use the browsers developer tools to inspect actual response to see the returned data – Arun P Johny Feb 12 '14 at 04:05
  • @FelixKling do you know any good blog to redirect guys about browser debugging – Arun P Johny Feb 12 '14 at 04:05
  • @ArunPJohny: I have this little snippet I use in comments: [Learn how to](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820) [**debug** JavaScript](https://developers.google.com/chrome-developer-tools/docs/javascript-debugging). I don't know off the top of my head how much it says about `console.log` though. – Felix Kling Feb 12 '14 at 04:07
0

Stop use alert to debug, use console.log instead.

success: function(data) {
  // in the browser console, you will see the data structure.
  // then do what you want
  console.log(data);
  // ...
xdazz
  • 158,678
  • 38
  • 247
  • 274