PHP+;AJAX:如何阻止javascript在发送/存储数据时摆脱操作符?
我试图发回一个字符串表达式,例如“3+3”,但当PHP收到它时,它显示为“3 3”。在线路的某个地方,操作员正在被删除PHP+;AJAX:如何阻止javascript在发送/存储数据时摆脱操作符?,javascript,php,ajax,Javascript,Php,Ajax,我试图发回一个字符串表达式,例如“3+3”,但当PHP收到它时,它显示为“3 3”。在线路的某个地方,操作员正在被删除 function sendEquation() { let str = document.querySelector(".equation").innerText; let xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (this.readyState
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);
encodeURIComponent()
函数将+
转换为%2B
。应该在服务器上正确解码
(具体来说,
+
代表空格字符,正如您所发现的。)您需要对+
进行编码,否则它将被转换为空格:xhr.open(“GET”,“Scripts/math.php?q=“+encodeURIComponent(str),true)代码>非常感谢!这一切都解决了。