我试图发回一个字符串公式,例如 "3+3",但当 PHP 收到它时,它显示为 "3 3"。沿着这条线的某个地方,运算符被擦掉了。
function sendEquation() {
let str = document.querySelector(".equation").innerText;
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
document.querySelector(".equation").innerHTML = this.responseText;
}
};
xhr.open("GET", "Scripts/Maths.php?q=" + str, true);
xhr.send();
}
我想可能是在这段代码中丢失了。如果我的假设是正确的,我如何阻止javascript解释我想让它存储的值,而只是给我发送写好的字符串?
的 +
在URL语法中意味着什么,所以你必须对它进行编码。
xhr.open("GET", "Scripts/Maths.php?q=" + encodeURIComponent(str), true);
The encodeURIComponent()
功能将使 +
变成 %2B
. 那 应 在服务器上被正确解码。
(具体来说。+
代表一个空间字符,你已经发现了)。)