javascript ajax php调用返回3个以上的字符
i have this php file (stripped down to the essential part)
getBoundaries.php
<?php
$t = "";
//$t = "t";
Header("Content-type: text/plain; charset=utf-8");
echo ($t);
?>
and this javascript ajax call:
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);
}
The output of console.log
with $t=""
were 32, 10, 32.
The output of console.log
with $t="t"
were 116, 32, 10, 32.
So, my question is: WHY isn't an empty string returned as responseText? Where are these additional 3 characters added? My assumption: the echo
php-command adds those three chars.
Any suggestions?
我有这个php文件(剥离到必要部分) p>
getBoundaries.php p>
&lt;?php
$ t =“”;
// $ t =“t”;
标题(“内容类型” :text / plain; charset = utf-8“);
echo($ t);
?&gt;
code> pre>
此javascript ajax调用:
function anyname(){
var xhttp;
var query =“tid = 1&amp; pid = 1”;
xhttp = new XMLHttpRequest();
xhttp .onreadystatechange = function(){
if(xhttp.readyState == 4&amp;&amp; xhttp.status == 200){
var temp = xhttp.responseText;
if(temp ==“”){\ n console.log(“ein leerer string”);
}
else {
for(i = 0; i&lt; 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);
}
code> pre>
控制台的输出。 $ t =“” code>的log code>是32,10,32。 p>
console.log code>的输出 $ t =“t” code>分别为116,32,10,32。 p>
所以,我的问题是:为什么不是作为responseText返回的空字符串? 这些额外的3个字符在哪里添加? 我的假设: echo code> php-command添加了这三个字符。 p>
有什么建议吗? p>
div>
charCodeAt()
returns the unicode value of the character:
10 -> 
 -> Line Feed
32 ->   -> Space
So after your echo you get space - line break - space added to your output buffer (probably after a closing ?>
tag)