2

I'm trying to get an array of objects with data from my database into jquery to render on a website.

ex: example.php

<?php
function testFunction() {

$data = array()
$mydata = new stdClass;
$mydata->example = 'test';
$data[] = $mydata
return json_encode($data);
}

echo testFunction();
?>

ex index.html

<script>
$.ajax({
                    type: 'POST',
                    url: 'example.php',
                    data: {map: map},   
                    cache: false,
                    dataType: 'json',                 
                    success: function(response) {
                      console.log(response[0].example);
                    }
});

</script>

Output:

console.log(response);

["test", $family: function, $constructor: function, each: function, clone: function, clean: function…]

console.log(response[0].example);

undefined

So essentially, I receive the response fine, when I log it it gives me a structure that makes sense. however I can't seem to find the correct way of accessing my objects inside the array, the example I have above only returns undefined. What is the correct syntax for this please?

FroboZ
  • 437
  • 1
  • 6
  • 17

3 Answers3

4

You need to JSON.parse(response); the response. You should be able to access it like you need then..

var parsed_reply = JSON.parse(response);

EDIT After actually looking at the code:

PHP

<?php

$data['example'] = "test";

echo json_encode($data);

?>

JAVASCRIPT

<script>
$.ajax({
                type: 'POST',
                url: 'example.php',
                data: {map: map},   
                cache: false,
                dataType: 'json',                 
                success: function(response) {
                  console.log(response['example']);
                }
});

</script>

OUTPUT: "test"

superphonic
  • 7,954
  • 6
  • 30
  • 63
  • var parsed_reply = JSON.parse(response); console.log(parsed_reply.example); throws: Uncaught TypeError: Illegal invocation – FroboZ Nov 12 '13 at 08:26
  • Sorry I brushed over your code quickly. You don't have a key called `example` in your response data. Your PHP function isn't returning an Indexed or Associative array, its simply returning "test". You need to json_encode a proper associative array. – superphonic Nov 12 '13 at 10:33
1

You need call testFunction() in example.php

<?php
   function testFunction() {

    $data = array()
    $mydata = new stdClass;
    $data[] = $mydata->example = 'test';

    return json_encode($data);
  }
     echo testFunction();
?>
Krish R
  • 22,583
  • 7
  • 50
  • 59
  • Sorry I omitted that part as it is only an example, as I said I do get the response filled with data, I just don't know how to access it properly. I will edit my post for clarity tho. – FroboZ Nov 11 '13 at 13:53
  • Please post your output response. – Krish R Nov 11 '13 at 13:55
  • console.log(response): ["test", $family: function, $constructor: function, each: function, clone: function, clean: function…] console.log(response[0].example): "undefined" – FroboZ Nov 11 '13 at 14:00
  • where is example object in your response? – Krish R Nov 11 '13 at 14:01
  • "test" is the variable stored in example property of the mydata object? – FroboZ Nov 11 '13 at 14:06
  • No you can't. 'test' is the value not the key. `result[0]` will return `test` – andy Nov 12 '13 at 12:29
1
function testFunction() {

$data = array();
$mydata = new stdClass;
$mydata->example = 'test';
$data[] = (array) $mydata;
return json_encode($data);
}

echo testFunction();

The response is:

[{"example":"test"}]

The key here is (array) $mydata which converts the stdclass to an array before putting it in $data

andy
  • 2,369
  • 2
  • 31
  • 50