Answer the question
In order to leave comments, you need to log in
Why does the error "Uncaught (in promise) SyntaxError: Unexpected end of JSON input" appear?
Hello. I've been sitting on Uncaught (in promise) SyntaxError: Unexpected end of JSON input all day. I'm trying to send form data to the mail using JS, PHP and the PHPMailer plugin. Below is the code for the fragments of receiving data from the form in JS, and then sending this data to PHP. Thank you in advance!
JS
async function formSend(e) {
e.preventDefault();
let error = formValidate(form);
let formData = new FormData(form);
if (error === 0) {
let response = await fetch('sendmail.php', {
method: 'POST',
body: formData
});
if (response.ok) {
let result = await response.json();
alert(result.message);
form.reset();
} else {
alert("Ошибка");
}
} else {
alert('Заполните обязательные поля');
}
$mail->Body = $body;
//Отправляем
if (!$mail->send()) {
$message = 'Ошибка';
} else {
$message = 'Данные отправлены!';
}
$response = ['message' => $message];
header('Content-Type: application/json');
echo json_encode($response);
Answer the question
In order to leave comments, you need to log in
I tried, but half refused to work, because the script dynamically substitutes values, etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question