Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 链接触发XMLHttpRequest_Javascript_Html_Xmlhttprequest - Fatal编程技术网

Javascript 链接触发XMLHttpRequest

Javascript 链接触发XMLHttpRequest,javascript,html,xmlhttprequest,Javascript,Html,Xmlhttprequest,我做了一个简单的XMLHttpRequest,它可以工作,它可以发送请求等,就像W3学校一样 var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demox").innerHTML = this.responseT

我做了一个简单的XMLHttpRequest,它可以工作,它可以发送请求等,就像W3学校一样

  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById("demox").innerHTML = this.responseText;
    }
  };

  xhttp.open("POST", "textx.php", true);
  xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhttp.send("fname=" + textxx);
}
当我试图通过单击链接触发请求时,问题就开始了,该链接将我发送到处理请求的php文件。我发现在我目前的水平上很难理解为什么它不起作用,因为它使用的是简单的表单之类的东西

我得到:

"Notice: Undefined index: fname ..."

所以,我想,这意味着变量没有被发送。有人能解释一下吗?或者有没有办法调试从一个页面发送到另一个页面的内容。我在chrome中找到的只是一个调试器,它确实捕获了请求,但没有实际用途,因为我被发送到textx.php页面,所有内容都丢失了。

不太确定,您的问题可能在哪里,请尝试:

var xhttp = new XMLHttpRequest();
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.open("POST", "textx.php", true);
    xhttp.onreadystatechange = function() {
    if (this.readyState === 4){
        if(this.status===200 || this.status===0){
            document.getElementById("demox").innerHTML = this.responseText;
        }
    };

    var fname = "fname=" + textxx;
    xhttp.send(fname);
}

您可以
console.log(xhttp)
并查看逐步配置文件,找出问题所在

无论哪种方式,我仍然不确定,但我将我的页面(代码)上传到了托管服务器,代码正常工作。PHP没有显示任何警告,一切都按计划进行。问题似乎与运行本地服务器(WAMP)有关。更改PHP版本没有帮助。我可能需要对此进行更深入的研究。

如果我通过触发JS代码,它确实会起作用,但是当我使用