Using jQuery to make an AJAX call to a PHP file results in the following error in Chrome only: SyntaxError: Unexpected token ILLEGAL
jQuery Snippet:
$.ajax({
type: 'POST',
url: 'ajax/Test_Get.php',
dataType: 'json',
data: {
ID: settings.ID
},
success: function(data) {
console.log('ok');
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
console.log('error');
});
PHP Snippet:
...
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-Type: application/json; charset=utf-8');
require_once('../classes/Database.php');
require_once('../classes/Test.php');
...
I spent hours researching this issue and there are a few posts on stackoverflow that talk about problem characters etc. but I haven’t been able to solve this.
The specific problem here appears to be having another <?php block on the page (whether it be as part of an include file or added directly to the page). So because the Test.php file has a <?php block in it I can’t include this class. Other browsers work just fine but not Chrome.
The only bad work around I’ve been able to find is to copy and paste the class code directly within the existing <?php block on the page. This just doesn’t seem right — there must be something fundamental I’m missing here.
The problem was caused by a single space before the opening php tag in the include file.