2

Below I have a AJAX get request. The index.php file called returns the data in JSON format. What I want to do is have that data put into a JavaScript array. Currently what I have works, however the array puts each different letter into an array.

$.get('../poll/index.php?data=vehicles', function(data) {
    for (var i = 0; i < data.length; i++) {
        vehicles.push(data[i]);
    }
    console.log(vehicles);
});

enter image description here

Am I forgetting to do something?

Thanks

jskidd3
  • 4,609
  • 15
  • 63
  • 127
  • You didn't not parse the response. See [How to parse JSON in JavaScript](http://stackoverflow.com/q/4935632/218196). If you'd set [the correct content type response header](http://stackoverflow.com/q/267546/218196), jQuery would parse it automatically for you. Alternatively you can pass `'json'` as fourth parameter to `$.get`. – Felix Kling Jul 08 '13 at 14:42
  • In the PHP file I set the response header: CURLOPT_HTTPHEADER => array('Content-type: application/json') – jskidd3 Jul 08 '13 at 14:46
  • That seems to be the header for a request made through curl though, not a header for the Ajax response. – Felix Kling Jul 08 '13 at 14:47
  • Ahh, so what do I do instead to correct that? – jskidd3 Jul 08 '13 at 14:48
  • @FelixKling is correct, the `data` you `get` is a `string` now. You should parse it into `JSON` or use the `json` as the fourth parameter. – Miaonster Jul 08 '13 at 14:48
  • As I said: (a) Parse the JSON, or (b) set the header properly (see http://php.net/manual/en/function.header.php), or (c) pass `'json'` as third/fourth argument to `$.get`. – Felix Kling Jul 08 '13 at 14:49
  • 1
    @Dylengleng: No, you don't parse it *into* JSON. The data *is* JSON. The JSON has to be parsed into an array (or object). – Felix Kling Jul 08 '13 at 14:50
  • @FelixKling Oh, yes, I have a wrong understanding of JSON. – Miaonster Jul 08 '13 at 14:58
  • `for (var i in vehicles){ vehicles[i] = vehicles[i].join(""); }` – Jeff Noel Jul 08 '13 at 14:59

3 Answers3

3

data is a string containing JSON, it is not an array. You have four options to convert the response (JSON) in a JavaScript data type (array or object):

  1. Parse the JSON explicitly: Parse JSON in JavaScript?

  2. Set the proper content type response header to let jQuery automatically parse the response for you: https://stackoverflow.com/q/267546/218196

  3. Tell jQuery about the format of the response, so that it parses it for you. For that, pass 'json' as the last argument to $.get.

  4. Use $.getJSON, which is equivalent to 3.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
1

Try this:

$.getJSON('../poll/index.php?data=vehicles', function(data) {
    for (var i = 0; i < data.length; i++) {
        vehicles.push(data[i]);
    }
    console.log(vehicles);
});
maketest
  • 281
  • 1
  • 8
0

Simply use jQuery.getJSON() instead of jQuery.get(). Currently your request expect to receive raw text, i.e. array of characters.

See the official documentations here and here, but in you case it's the same use

Ricola3D
  • 2,402
  • 17
  • 16