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>