Requesting JSON from php script :
var channelList;
$(document).ready(function() {
$.ajax({
url: 'channellookup.php',
dataType: 'json',
error: function(){console.log(arguments)},
success: function(data) {
console.log(data.success);
channelList = data;
}
});
});
Now to the interesting part: The error message in the console reads like this:
Arguments { 0: XMLHttpRequest, 1: "parsererror", 2: "Invalid JSON: <?php
header('Content-type: application/json'); // To ensure output json type.
class MyDB extends SQLite3
{
And so on. My whole PHP code is in that message. Something must go completely wrong here.
Here is my PHP in full
<?php
header('Content-type: application/json'); // To ensure output json type.
class MyDB extends SQLite3
{
function __construct()
{
$this->open('database_sqlite3.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully
";
}
$sql =<<<EOF
SELECT * from channels;
EOF;
$ret = $db->query($sql);
$channelList = array();
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
$channelList[] = $row;
}
echo json_encode($channelList);
$db->close();
?>
as you can see I do encode as json. As I said this works in the terminal. Please keep in mind that I do not want to use the json in my html yet so the page generating before the asynchronous request is completed isn't an issue yet.
Is the problem maybe that I am not doing this on a remote server but on local files? As I understand the Browser should be able to handle this case.