问题
I'm using an ajax request to send comments to DB. Succesful response is marked by
1. OK
The problem actually is that the response from the php script is
1.
2. OK
So I debugged the script and noted that the newline character si being added when the script executes the following line:
require_once($ABS_APPS."/quotes/classQuote.php");
After some searches i read that it could be a BOM (Byte Order Mark) problem. So I just downloaded and opened the classQuote.php
file with an hex editor and noticed that there's no BOM... can someone help me?
P.S. All files in my project are encoted in UTF-8, and I'm currently usint NetBeans which doesn't add BOM to files.
This is the incriminated script:
// 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;
回答1:
- Make sure there is nothing, preceeding the opening
<?php
in your classQuote.php - Make sure there are no trailing characters / lines after the closing
?>
- Check to see if a
?>
tag exists somewhere in the lines of code (follow flow from your __construct and where you invoke stuff)
Infact, it could prove helpful to leave out the closing tag. Another possibility is this:
// 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());
}
回答2:
If you're using jQuery
you can use jQuery.trim(responseData) in your AJAX success callback, to get rid of the white spaces
see also here http://api.jquery.com/jQuery.trim/
hope it helps
回答3:
I fixed by these
- Make sure there is nothing, preceeding the opening
- Make sure there are no trailing characters / lines after the closing ?>
- Check to see if a ?> tag exists somewhere in the lines of code (follow flow from your __construct and where you invoke stuff)
来源:https://stackoverflow.com/questions/11593326/unwanted-new-line-returned-after-ajax-request