[英]Removing white-space in AJAX response
I have a php script which returns 0.28
.我有一个返回
0.28
的 php 脚本。 This is then fetched to HTML using AJAX, and inserted into a span.然后使用 AJAX 将其提取到 HTML,并插入到跨度中。 The problem is, it is inserted with 5 spaces and what appears to be a newline.
问题是,它插入了 5 个空格,并且看起来像是一个换行符。 jQuery then sees that as a change and updates the existing span with the new value when there is no change.
jQuery 然后将其视为更改,并在没有更改时使用新值更新现有跨度。 It appears that the whitespace does not come from the php. I have tried:
看起来空格不是来自 php。我试过:
Dumping the php variable:转储 php 变量:
string(4) "0.28"
This is how my browser returns it这是我的浏览器返回它的方式
<span id="response1" style="display: inline;">*5spaceshere*
0.28</span>
This is my script这是我的脚本
$(function () {
function update() {
$.ajax({
type: "GET",
url: "balancefetch.php",
dataType: "html",
success: function (response) {
var trimmedResponse = $.trim(response)
if (trimmedResponse != $("#response1").html()) {
$("#response1").fadeOut(200, function () {
$("#response1").html(response);
$("#response1").fadeIn();
});
} else {}
}
});
}
setInterval(update, 5000);
update();
});
And my html还有我的 html
<div id="balance-div"><!--
--><p class="balance">Balance<span id="response1">Loading...</span></p><!--
--></div>
php php
$balance = ltrim($balance);
echo $balance;
$balance
holds 0.26 $balance
持有 0.26
So after testing and going trough several methods finding where the problem is, the source for this problem was found. 因此,经过测试并通过几种方法找到问题所在,找到了这个问题的根源。
The main part why not using technics like trim
or a RegEx to remove whitespaces is, because that would fix the current problem, but not the source, so this would be just a bad workaround, so it was important to find the source. 为什么不使用
trim
或RegEx之类的技术来删除空格的主要部分是,因为这会解决当前问题,而不是源,所以这只是一个不好的解决方法,所以找到源是很重要的。
The source problem was inside the configuration file, which had some whitespaces inside it and after including it inside the script, which is loaded via AJAX, the whitespaces also were included. 源代码问题出在配置文件中,配置文件里面有一些空格,并且在将其包含在脚本中之后(通过AJAX加载),也包含了空格。
Solution : Remove the whitespaces of the config file and/or just remove the ?>
PHP-Ending Tag. 解决方案 :删除配置文件的空格和/或只删除
?>
PHP-Ending标记。
For further debuging steps, look into the comment section of the question. 有关进一步的调试步骤,请查看问题的评论部分。
solution found ! 找到解决方案
The problem was in the php file. 问题出在php文件中。 I should not keep any white spaces between lines in my php file
我不应该在我的php文件中的行之间保留任何空格
here is my php file and i indicated in a comment where the white space was 这是我的php文件,我在评论中指出白色空间
<?php
require_once '../includes/session.php';
require_once '../includes/functions.php';
require_once '../includes/validation_functions.php';
// white space is in the place of this comment here ! so i removed it and problem was solved
if (isset($_POST['reply_board_textarea'])) {
$board_id = (int)$_GET['board_id'];
$comment_id = (int)$_GET['comment_id'];
$reply = mysql_prep($_POST['reply_board_textarea']);
$reply_timestamp = time();
$user_id = (int)$_SESSION['user_id'];
// INSERT into table
$query = "INSERT INTO board_comment_reply_table ( ";
$query .= "comment_id, board_id, user_id, reply, reply_timestamp";
$query .= ") VALUES (";
$query .= " $comment_id, $board_id, $user_id, '$reply', $reply_timestamp";
$query .= ")";
$result = mysqli_query($connection, $query);
$reply_count = board_reply_count($comment_id);
echo $reply_count;
}
?> ?>
This happened to me too today (two line breaks always appeared in front of my ajax responses).这也发生在我今天(我的 ajax 响应前面总是出现两个换行符)。
After finding this discussion, I started reviewing my code.找到这个讨论后,我开始审查我的代码。 Finally, I found the two-space newline after the closing tag of php?> in a.inc file that was included in my code.
最后,我在我的代码中包含的 a.inc 文件中找到了 php?> 的结束标记后的两个空格换行符。
Many Thanks非常感谢
Use ob_clean()
which clears buffer that had unwanted newline使用
ob_clean()
清除缓冲区中不需要的换行符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.