gpt4 book ai didi

javascript - 错误: "JSON.parse: unexpected character at line 1 column 1 of the JSON data" when include additional php file

转载 作者:行者123 更新时间:2023-12-03 11:12:47 25 4
gpt4 key购买 nike

我有一个问题,无法理解我做错了什么:

  • 一个buttonindex.php通过 onClick.js 提交数据至userZone.php
  • userZone.php将创建一个字符串数组并编码为 JSON
  • onClick.js将显示解码后的JSON结果为警报。

这工作正常,直到我改变 userZone.php简单的一行:

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

至:

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

-> db.php应包含进一步的数据库连接设置,但当前是一个空文件。

一旦我包含空文件 db.php ,Firebug 告诉我:

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

完整的项目可以在这里找到: http://meet24.ch/StackOverFlow.zip

最佳答案

您的问题是您将文件保存为带有 BOM 的 UTF8。

虽然文件的开头看起来像,但 <? 之前没有任何字符:

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

实际上有一个Byte Order Mark前面<?这个字符不可见,但会被解析。正如 <? ?> 之外的所有内容一样将包含在输出中,BOM 将与服务器的响应一起发送。

这不仅会破坏 JSON 解析,还会破坏压缩(在 php 中完成时),因为它会阻止发送 header 。在服务器日志中,您还应该看到类似 Warning: Cannot modify header information - headers already sent by 的内容。这是BOM造成的。

使用 php 时,应始终确保将文件保存为 UTF8 不带 BOM。

关于javascript - 错误: "JSON.parse: unexpected character at line 1 column 1 of the JSON data" when include additional php file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27494244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com