2

For example, I am json_encoding my array and outputting it:

$key = $this->key;
                    $sql = "SELECT * FROM Requests";                   
                    $result = $this->db->query($sql);
                    $temp = array();
                    foreach($result as $r)
                    {
                        if($r['key'] == $key){
                        array_push($temp, array(
                            'song' => $r['song'],
                            'artist' => $r['artist'],
                            'by' => $r['by']
                        ));
                        }
                    }
                    $this->encoded = json_encode($temp);
                    echo $this->encoded;

Then, I am sending a GET request to it:

$.get('http://www.example.com/file.php')
                .done(function(data){
                    alert(data['artist']);
                });

However, this alerts:

undefined

Can anyone possibly help me? I've searched a lot and tried a lot but nothing seems to be working (like, JSON.Parse).

Edit: I tried adding the header('Content-Type: application/json'); but it leaves me with this error:

TypeError: data is null


Thanks in advance!

$.get('http://www.syncsapi.x10host.com/API/Public/', {
  start: 'example'
})
$.get('http://www.syncsapi.x10host.com/API/Public/', {
    display: '5'
  })
  .done(function(data) {
    console.log(data[0]);
  });
});
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

20
  • Possible duplicate of Returning JSON from a PHP Script Commented Mar 17, 2016 at 0:48
  • that returns: TypeError: data is null now Commented Mar 17, 2016 at 0:51
  • Can you share the exact output of that php script? Commented Mar 17, 2016 at 1:04
  • that is the exact output, but when i echo "test"; it works because data is set as "test" :/ just, when i echo the json_encoded or array, the data is undefined or null Commented Mar 17, 2016 at 1:08
  • 1
    No the output of your PHP script, as your webserver emits it. Not the output of your javascript. Commented Mar 17, 2016 at 1:09

1 Answer 1

4

So the problem with your script is that you are basically emitting something like this:

[
  {
    "song"   : "...",
    "artist" : "..."
  }
]

But your javascript code is expecting something like this:

{
  "song"   : "...",
  "artist" : "..."
}

If your intention was to only ever send back one song for that PHP service, you should modify your PHP service to remove the top-level array.

However, if you intend to send back more than 1 song, then the data you returned makes sense, but your javascript does not. To fix your javascript, you could simply change:

alert(data['artist']);

into:

alert(data[0].artist);

Note that .artist and ['artist'] is the same, the real difference in my sample is that I added [0] which grabs the first item in the top-level array.

Sign up to request clarification or add additional context in comments.

11 Comments

with the header mentioned below: error - TypeError: data is null without it: error - data[0] is undefined :/
@KyleE4K I'm thinking then there might still be something wrong with your php script. Is it possible at all to send the url so I can take a look?
$.get('http://www.syncsapi.x10host.com/API/Public/', { display: '5' }) .done(function(data){ console.log(data[0]); alert(data[0].artist); });
Awesome. Interestingly your PHP script emits nothing! Empty string.
Verify by just opening syncsapi.x10host.com/API/Public/?display=5 in a browser.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.