2016-01-15 15 views
0

我有這個PHP文件(剝離下來的重要組成部分)JavaScript的AJAX PHP調用返回3個字符

getBoundaries.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); 
} 

console.log$t=""的輸出爲32,10,32。

console.log$t="t"的輸出是116,32,10,32。

所以,我的問題是:爲什麼不是一個空字符串作爲responseText返回?這些額外的3個字符添加在哪裏?我的假設:echo php-command添加了這三個字符。

有什麼建議嗎?

+1

也許你'>>'後面有一些空格(結束標記) –

+1

另外,請注意結束標記不是強制性的。當然,如果在同一頁面上使用PHP和HTML代碼,則必須使用它.. –

+1

看起來更像是一個空格之後?>然後是一個帶有空格的新行 –

回答

1

charCodeAt()返回字符的Unicode值:

10 -> &#010; -> Line Feed 
32 -> &#32; -> Space 

所以,你的迴音後,你得到的空間 - 換行符 - 空間添加到您的輸出緩衝器(可能關閉?>標籤後)

+0

非常感謝您的建議。在關閉'?>'標籤之後刪除剩餘的行/空格就可以實現。 – Bardock