GET-запрос работает:
var xhr = new XMLHttpRequest;
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.querySelector('.response').innerHTML = this.responseText;
}
}
xhr.open("GET", "search.php?q="+val, true);
xhr.send();
PHP (GET):
header("Content-type: text/plain; charset=windows-1251");
if(ISSET($_GET)) {
$val = $_GET['q'];
echo $val;
} else {
echo 'Not GET variables';
}
POST:
var xhr = new XMLHttpRequest;
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.querySelector('.response').innerHTML = this.responseText;
}
}
xhr.open("POST", "search.php", true);
xhr.send(val);
PHP:
header("Content-type: text/plain; charset=windows-1251");
if(ISSET($_POST)) {
$val = $_POST['val'];
echo $val;
} else {
echo 'Not POST variables';
}
JS полный:
var val;
var inp = document.querySelector("#query-input");
inp.onkeyup = function () {
val = inp.value;
if (val.length != 0) {
var xhr = new XMLHttpRequest;
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.querySelector('.response').innerHTML = this.responseText;
}
}
xhr.open("POST", "search.php", true);
xhr.send(val);
}
}
Не работает POST-запрос. Нет вовсе никакого ответа. Т.е. запрос-то есть, но сервер не отвечает, хотя переменная val определена. Как правильно сделать запрос?