0

I have a function that fetches data from a server via an AJAX request. The asynchronous callback takes the data and then displays it to the DOM.

I'm aware that the getJSON AJAX request is supposed to return an array of strings (and is automatically parsed for my use). So I want to take that array of strings and cycle through them using jQuery's $.each method, which takes 2 arguments - the collection and a function that operates on each item in the collection.

Here's the relevant code:

setInterval(function () {
            $.getJSON("https://api.parse.com/1/classes/chats")
            .done(function(dataReceived){  
                $('.messages li').remove();
                $.each(dataReceived.results, function (index, value) {
                    $('.messages').append('<li>' + value.text + '</li>')
                })    
            });
        }, 2500);

My question is with regards to the array that's returned from the getJSON request - in my function it's a parameter named 'dataReceived'...

That's an array of strings that's already been parsed so it's Javascript ready, right?

Also, and this is the main crux of my question - I wasn't aware that there's a 'results' property on a Javascript array? Or is it only that the getJSON function returns an object/array that has a results property? I'm a little bit lost here...If anyone could clear that up for me (whether by directly answering or with the appropriate documentation) that would be great. Because the code doesn't work if I just pass 'dataReceived' into the $.each-iterator. It requires the 'results' property (dataReceived.results).

Seeeyonnn
  • 315
  • 2
  • 10
  • 19
  • There won't be any named properties in a JSON `Array`, but it can be in an `Object` that does. Such as: `{"results": [...]}`. "*I'm aware that the getJSON AJAX request is supposed to return an array of strings [...]*" No. The remote resource may return an array of strings, but JSON and `getJSON()` [support more variations than that](http://json.org/). – Jonathan Lonowski Feb 24 '14 at 21:57
  • I suppose it depends on the server then huh. So it's probably the server that's sending back an array that has the results.property? – Seeeyonnn Feb 24 '14 at 22:03
  • no, Arrays don't have a results property. the only properties they have are length, __proto__, and constructor, the others are all methods. length is the only one they actually own... – dandavis Feb 24 '14 at 22:09
  • Well, yes and no. It does depend on the server and how it responds. But, [Parse is responding](https://parse.com/docs/rest#queries-basic) with an object (`{...}`) that has a `"results"` property. The value of that property is the array (`[...]`) you're iterating. – Jonathan Lonowski Feb 24 '14 at 22:17
  • ^ Ok @Jonathan Lonowski I think that answers it. Feel free to put that as an answer as opposed to comment and I'll accept – – Seeeyonnn Feb 24 '14 at 22:23

1 Answers1

2

I'm aware that the getJSON AJAX request is supposed to return an array of strings (and is automatically parsed for my use).

Not entirely. jQuery.getJSON() will parse a JSON response into the equivalent JavaScript instances and values. But, what those values are depends entirely on the response.

I wasn't aware that there's a 'results' property on a Javascript array?

In this case, since the request is to Parse, their basic query responses follow the format you're finding (amended with comments):

The result value is a JSON object that contains a results field with a JSON array that lists the objects.

{               // root object
  "results": [  // array of results
    {           // individual result object
      // ...
    },
    {           // another result object
      // ...
    }
  ]
}

In the callback, dataReceived will be equal to the "root" Object defined by parsing such JSON data.

Community
  • 1
  • 1
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • Not sure if you're still there @Jonathan Lonowski, but as a quick, related side-question - at what point in the code is the JSON object parsed back into something Javascript can work with? Is it as soon as the server returns a response? Or as soon as the .done callback is executed? – Seeeyonnn Feb 24 '14 at 22:37
  • jQuery parses it, and depending on the result of that parse executes either the done or fail callback, and then the always callback. – Kevin B Feb 24 '14 at 22:41