我有这个php文件(剥离到基本部分)
获取边界.php
<?php
$t = "";
//$t = "t";
Header("Content-type: text/plain; charset=utf-8");
echo ($t);
?>
这个JavaScript Ajax调用:
function anyname(){
var xhttp;
var query = "tid=1&pid=1";
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var temp = xhttp.responseText;
if (temp == ""){
console.log("ein leerer string");
}
else{
for (i = 0; i < temp.length; i++){
console.log(temp.charCodeAt(i));
}
}
}
}
xhttp.open("POST", "getBoundaries.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(query);
}
带$t=""
的console.log
产量分别为32、10、32。
$t="t"
console.log
的产量分别为116、32、10、32。
所以,我的问题是:为什么空字符串不作为响应文本返回?这额外的 3 个字符在哪里添加?我的假设:echo
php-命令添加了这三个字符。
有什么建议吗?
charCodeAt()
返回字符的 unicode 值:
10 -> 
 -> Line Feed
32 ->   -> Space
因此,在回显之后,您会得到空格 - 换行符 - 添加到输出缓冲区的空间(可能在结束?>
标签之后)