N-iceman
N-iceman

Reputation: 61

Error: "JSON.parse: unexpected character at line 1 column 1 of the JSON data" when include additional php file

I have an issue and cannot understand what I do wrong:

This works fine until I change in userZone.php a simple line from:

//include($_SERVER['DOCUMENT_ROOT']."/engine/db.php"); 

to:

include($_SERVER['DOCUMENT_ROOT']."/engine/db.php");

-> db.php should include further DB connection settings but is currently an empty file.

As soon as I include the empty file db.php, Firebug tells me:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data var json = JSON.parse(data);

The full project can be found here: http://meet24.ch/StackOverFlow.zip

Upvotes: 0

Views: 2621

Answers (3)

David
David

Reputation: 953

The file db.php is not empty. Here are the non-comment contents:

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 01 Jan 1996 00:00:00 GMT');
header('Content-type: application/json');
header('Content-Type: text/html; charset=utf-8');

session_start();

The header calls tell the server to send specific HTML header lines. In this case, they tell the browser that the page is a HTML page, using character set UTF-8, that should not be cached.

session_start tells the server that you want to use the $_SESSION object. This function call should be the first thing you do. If you have even so little as an extra space or a UTF Byte-order marker you will get an error message: "headers already sent".

In your project, you include db.php in the file userZone.php on row 8. But before that you have sent headers. So headers get sent twice, once in userZone.php and once in db.php. Then you call session_start in db.php. You don't use the session variable in the project so it seems pointless. This is probably not what causes your error though. The code near the end of userZone.php doesn't work as you might expect either.

Upvotes: 0

t.niese
t.niese

Reputation: 40852

Your problem is that you saved your file as UTF8 with BOM.

While the beginning your file looks like, not having any char before <?:

<?
  // ####################################################################################################################
  header('Cache-Control: no-cache, must-revalidate');

There is actually a Byte Order Mark in front of <? this chars are not visible but will be parsed. As everything that is outside of the <? ?> will be included into the output, the BOM will be send with the response from your server.

This will not only break JSON parsing, but also compression (when done in php) as it prevents headers from being send. In you server logs you should also see something like Warning: Cannot modify header information - headers already sent by which is cause by the BOM.

When working with php you should always make sure to save file as UTF8 without BOM.

Upvotes: 1

sergolius
sergolius

Reputation: 438

Check '$request' - looks like it is undefined. Try to use this:

 if ($_POST['request'] == 'jsonTest')

insteadof:

 if ($request == 'jsonTest')

Check response from ajax.

Upvotes: 0

Related Questions