所以我有一个服务器端ajax php文件:

$thing = "\n\ncode\n";
echo(trim($thing, '\n'));




但是,当我使用此php文件进行ajax调用时,responseText不会删除换行符!



var xhr7 = new XMLHttpRequest();
xhr7.onreadystatechange = function() {
  if (xhr7.readyState == 4) {
    if (xhr7.status == 200) {
      alert('trying');
      alert(xhr7.responseText);
      chrome.tabs.executeScript(null, {code: xhr7.responseText });
    } else {
      alert("404 server side ajax file DOES NOT EXIST");
    }
  }
};
xhr7.open('POST', 'http://texthmu.com/Devin/HMU%20ext/Post-jsfile-forItsCode.php', true);
xhr7.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //following w3
xhr7.send(); `

最佳答案

\ n必须使用双引号而不是单引号

echo(trim($thing, "\n"));


当值用单引号引起来时,它将被解析为文字。在这种情况下,您要修剪的是\n而不是换行符。

10-07 18:31