Отправка формы из GitHub pages на PHP хостинг с помощью AJAX(XMLHttpRequest)
Есть форма обратной связи:
<form id="contact_form" action="https://url.000webhostapp.com/form/configs/form.php" method="POST">
<div class="form-first">
<input type="text" name="name" value="">
<input type="text" name="email" value="">
</div>
<input type="subject" name="subject" value="">
<textarea name="message" rows="8" cols="80"></textarea>
<input type="submit" name="submit" value="Submit">
</form>
XMLHttpRequest:
const form = document.getElementById('contact_form');
form.addEventListener('submit', function(e) {
e.preventDefault();
const xhr = new XMLHttpRequest();
const action = document.getElementById('contact_form').getAttribute('action');
xhr.open('POST', action, true);
let data = new FormData(form);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onload = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(xhr.responseText);
};
};
xhr.send(data);
});
form.php(скрипт не итоговый, добавил эти строки, чтобы проверить работоспособность):
<?php
$object = file_get_contents('php://input');
$request = json_decode($object);
echo $request;
?>
Но ничего не работает и я получаю следующую ошибку:
POST https://url.000webhostapp.com/form/configs/form.php net::ERR_HTTP2_PROTOCOL_ERROR
Консоль показывает ошибку в предпоследней строке (xhr.send (data);).
Есть идеи как можно исправить? Обычный POST запрос с сабмитом работает, а вот AJAX не хочет.


