responseText contains extra whitespace characters (new lines, line feeds), how to prevent and remove them?

风流意气都作罢 提交于 2019-12-01 01:04:52

I read that these characters are added on by php, but I also read that the extra characters are different among different php versions/servers.

That's wrong. That's simple to verify: create a test.php file, write this and only this: <?php echo "test"; (without ?>) into it and execute it. There will be no whitespace.

These whitespaces most probably come from your scripts. A common error is to leave some trailing newlines after a closing php tags (?>), which results in a new line being printed.

Verify that all files included before or after you do echo "yes"; don't echo anything and don't have a trailing newline after a ?>.

The easiest way to avoid this problem is do not use php close tags at end of files (they are not mandatory).

I know you asked about doing the comparison without regexes, but given the conditions you mentioned above, that's going to be a very quick and effective way to get your answer, and won't necessarily perturb any other processing.

var trimmedResponse = responseText.replace(/^\s*/,'').replace(/\s*$/,'').toLowerCase();
if (trimmedResponse == 'yes') {
  // do your 'yes' case
} else if (trimmedResponse == 'no') {
  // do your 'no' case
} else {
  // do your 'none of the above' case
}

That's going to trim off leading white space, trailing white space (including the CR/LF combo), and convert to lower case just for comparison.

I think you may be going at it the wrong way. Instead of manually trying to create a response why don´t you use PHP arrays as a data structure and JSON for delivery?

<?php
$flag = false
if (condition){
  $flag = true;
}

$arr = array("is_true" => $flag)
$json = json_encode($arr);

// See http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
// Set JSONP header
header('content-type: application/json; charset=utf-8');

// Get callback from $_GET and prepend the JSON data
echo isset($_GET['callback'])
    ? "{$_GET['callback']}($json)"
    : $json;

Did you try using PHP's trim() function before sending the data?

Even if it's possible to look after all PHP script to avoid CRLF before or after php tag, this is not a good option as you can imagine adding in a few days, or month or years (without wanting!) a CRLF in a file and this will impact another part of your web site.

In fact, to avoid this problem you have two options:

Option 1: Let php send the data so with the garbage at beginning, and clean the data in Javascrpt with:

 response = http.responseText;
 response = response.replace(/[\n\r]+/g, '');

In this case this means you clean ALL the CRLF (change the Regex to clean only at beginning if needed or to clean also spaces)

Option 2 (better I think) is to clean the output buffer of PHP immediatly before sending the data to browser. So:

 ... many code here
 ob_end_clean();
 echo $data_for_the_browser;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!