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/[email protected]&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>У меня есть код, как показано выше, и операции ajax приводят к сбою. Что не так с моим кодом? Текст в p никогда не меняется. Файл php начинается так:
<?php
$mail = $_POST["mail"];
$password = $_POST["password"];
Есть ли ошибки в консоли веб-разработчика браузера?



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


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/[email protected]&password=abc", true);
xhttp.send();
}
из-за xhttp.open и send определены вне функционального блока.
xhttp.open("POST", "abcdef.xyz/abc/[email protected]&password=abc", true);
xhttp.send();
вы определили xhttp внутри функции?