错误:“JSON.parse:JSON数据第1行第1列的意外字符”,包含其他php文件

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

  • A button on index.php submit data over an onClick.js to userZone.php
  • userZone.php will create a string array and encode as JSON
  • onClick.js will show the decoded JSON result as alert.

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

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

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

insteadof:

 if ($request == 'jsonTest')

Check response from ajax.

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.

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.