简体   繁体   English

AJAX请求失败

[英]AJAX request failed

 function ajax() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = xhttp.responseText; alert(this.responseText); } }; } xhttp.open("POST", "abcdef.xyz/abc/logincheck.php?mail=abc@gmail.com&password=abc", true); xhttp.send(); 
  <p id="demo"> The content of the body element is displayed in your browser. </p> <button onclick="ajax()"> Click on me! </button> 

I have a code as seen above and the ajax operations do result in failure. 我有一个如上所示的代码,ajax操作确实导致失败。 What is wrong with my code? 我的代码出了什么问题? The text in p never changes.The php file starts like this: p中的文本永远不会改变.php文件的开头如下:

<?php
$mail = $_POST["mail"];
$password = $_POST["password"];

xhttp.open() and xhttp.send() need to be inside the ajax() function. xhttp.open()xhttp.send()需要在ajax()函数内。

function ajax() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML = xhttp.responseText;
      alert(this.responseText);
    }
  };
  xhttp.open("POST", "abcdef.xyz/abc/logincheck.php?mail=abc@gmail.com&password=abc", true);
  xhttp.send();
}

due to xhttp.open and send are define out side of function block. 由于xhttp.open和send都定义了功能块的一面。

xhttp.open("POST", "abcdef.xyz/abc/logincheck.php?mail=abc@gmail.com&password=abc", true);
xhttp.send();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM