我正在使用ajax请求将评论发送到数据库。成功响应标有
1. OK
问题实际上是来自PHP脚本的响应是
1.
2. OK
所以我调试了脚本,并注意到脚本执行以下行时添加了换行符si:
require_once($ABS_APPS."/quotes/classQuote.php");
经过一些搜索,我读到它可能是BOM(字节顺序标记)问题。因此,我刚刚使用十六进制编辑器下载并打开了classQuote.php
文件,并注意到没有BOM ...有人可以帮助我吗?
P.S。我项目中的所有文件都包含在UTF-8中,目前我使用的是NetBeans,它不会向文件添加BOM。
这是该脚本:
// Send new comment to DB
case "send":
$notification = new Notification();
if($comment->insert($_POST["username"], $_POST["comment"], $_POST["app"], $_POST["entryId"])){
switch ($_POST["app"]) {
case "quotes":
require_once($ABS_APPS."/quotes/classQuote.php");
$quote = new Quote();
$quoteData = $quote->get($_POST["entryId"]);
// If user comments his own entry we don't have to send the notification
if($quoteData["UserAuthor"] != $_SESSION["User"]){
$notification->newComment($_POST["username"], $quoteData["UserAuthor"], $_POST["entryId"], $_POST["app"]);
}
break;
default:
break;
}
echo "OK";
} else {
echo "ERROR";
}
break;
<?php
开头?>
之后没有尾随的字符/行?>
标记(遵循__construct的流程以及您在其中调用的内容)事实上,省略结束标记可能会有所帮助。另一个可能性是:
// capture output
ob_start();
require_once($ABS_APPS."/quotes/classQuote.php");
$quote = new Quote();
$quoteData = $quote->get($_POST["entryId"]);
// If user comments his own entry we don't have to send the notification
if($quoteData["UserAuthor"] != $_SESSION["User"]){
$notification->newComment($_POST["username"], $quoteData["UserAuthor"], $_POST["entryId"], $_POST["app"]);
// trim whitespace
echo trim(ob_get_clean());
}
如果您使用的是jQuery
您可以在AJAX成功回调中使用jQuery.trim(responseData),以摆脱空白
另请参阅此处http://api.jquery.com/jQuery.trim/
希望有帮助
我固定了这些
我也面临同样的问题。我想出的最终解决方案如下
检查所有已加载到向其发送AJAX请求的文件中的文件
首先删除多余的空格
如果它们是PHP文件,则不要在末尾添加?>。
4尝试使最后一行标记仅保持到所写代码的最后一行。表示不要在文件末尾添加额外的空格。如果代码在第32行结束,请勿通过编辑器转到第33行。实际上,按返回按钮并清除下面的所有内容,直到第32行为止。