【问题标题】:XMLHttpRequest and PHPXMLHttpRequest 和 PHP
【发布时间】:2017-02-19 00:00:25
【问题描述】:

我从表单中获取输入。输入包含简单的算术运算,如 2+3。据我所知,输入是正确的。这里块有来自表单输入字段的一小部分输入。正如您所看到的,当 alert(chuck) 给出 2+3(正确的东西)时,我将“2+3”作为参数发送到 calculate.php 后得到的响应是 2 3(2 空格 3)。我的 calculate.php 只是有类似 echo $_GET['param'] 的东西。

我在这个话题上没有太多经验。这里有什么问题?

chunk = theInput.substring(0,3);
alert(chunk); //output is 2+3
document.getElementById("txtHint3").innerHTML = chunk;//output is 2+3
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200) {
    var response = this.responseText;
    alert(response);  // But here output is 2 3
    document.getElementById("txtHint223").innerHTML = response;  // And same here  is 2 3               
        }
};
xmlhttp.open("GET", "calculate.php?param="+chunk,true);
xmlhttp.send();

【问题讨论】:

  • 您应该使用 jQuery 等框架,而不是尝试手动执行此操作。您的代码不太可能适用于所有浏览器。
  • 查看您的 PHP 代码可能也很有用,因为它在这里完成了大部分工作

标签: javascript php ajax xmlhttprequest


【解决方案1】:

+ 表示查询字符串中的空格。使用encodeURIComponent() 正确编码值:

xmlhttp.open("GET", "calculate.php?param="+encodeURIComponent(chunk),true);

【讨论】:

猜你喜欢
  • 2012-04-24
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
相关资源
最近更新 更多