Unwanted new line returned after AJAX request
Asked Answered
S

4

6

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;
Shaffer answered 21/7, 2012 at 15:13 Comment(4)
After asking over 30 questions, you should know to give some code in your question.Catalyze
@Truth I received another critic comment by another user because there was "too much code" (actually there were only 20 lines). By the way I'm adding more code.Shaffer
Depending on the situation, the code you post here may be a one-liner, or 30 lines. The code snippet should be concise, and relevant to the problem. Don't throw your entire app at us, but also don't leave us with nothing :)Catalyze
Mark your own answer as solution or give credit to one of the others to close the question pleaseVitriolic
V
12
  1. Make sure there is nothing, preceeding the opening <?php in your classQuote.php
  2. Make sure there are no trailing characters / lines after the closing ?>
  3. 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()); 
            }
Vitriolic answered 21/7, 2012 at 15:21 Comment(3)
Already checked using NetBeans find function... there is nothing before or after php tags :( - I also tried removing the ?> closing tag but nothing happened.Shaffer
try, in the bottom of your required file, put error_reporting(E_ALL); header("Foo: Bar"); - it will fail unless youre using output buffering. Iterate upwards in that file, putting the line on different locations - youre having something printed out, somewhereVitriolic
note that with the above commented method of locating the most recent output, you may get the line of the first output that 'broke' the headerspace with some additional newline or character of sorts. First line of output the needed buffering sorta say.Vitriolic
H
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

Helmut answered 22/7, 2012 at 9:0 Comment(0)
M
2

I also faced the same problem. The final solution I figured out is following

  1. Check all the files which are loaded into the file to which you are sending AJAX request

  2. Remove the extra white spaces from the very first <? line and PHP starting tag If your file starts from line number 1, the first ever character should be this <?.

  3. If they are PHP files, then don't add ?> at the end.

  4. Try to keep the last line marker only up to the last line of code written. means that do not add extra spaces at the end of the file. If code finishes at line 32, do not go to line number 33 via editor. In fact, press the back button and clear everything below up to line number 32.

  5. If you are using any PHP framework, then make sure that the hierarchy of all files loaded should not have the same problem. e.g. if you are using codeigniter framework them make sure that the calling function's controller and all the models loaded into that controller should not have the same problem.

Merrick answered 31/5, 2020 at 13:34 Comment(0)
C
1

I fixed by these

  1. Make sure there is nothing, preceeding the opening
  2. Make sure there are no trailing characters / lines after the closing ?>
  3. Check to see if a ?> tag exists somewhere in the lines of code (follow flow from your __construct and where you invoke stuff)
Clinquant answered 17/9, 2015 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.