I'm currently trying to use Ajax to use my php array in javascript. Even though I have json encoded the array, set the php content-type, and ajax datatype, it looks like javascript is still trying to process my php script instead of the json it outputs. This is because I always get a 'Unexpected Token <' error (the beginning of my php script).
Here is my ajax:
$.ajax({
type: 'GET',
cache: false,
url: 'api.php',
dataType: "json",
error: function(jqXHR, textStatus, errorThrown) {alert(errorThrown);},
success: function(data) {
alert(data);}
});
And here's my php (filename is api.php):
<?php
header('Content-Type: application/json');
$aliases = array('angry','birds');
echo json_encode($aliases);
?>
I know my php outputs the correct json format, because when I run it in my browser, the output is ["angry","birds"]
Can't seem to figure out what's going on.
http://localhost/api.php
– Dan Lee Oct 20 '13 at 1:30