C
C
ChipiLina2021-11-14 22:07:27
PHP
ChipiLina, 2021-11-14 22:07:27

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('Заполните обязательные поля');
    }


PHP
$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

3 answer(s)
T
ThunderCat, 2021-11-14
@ThunderCat

Network tab: What is sent, what do we receive in response?

N
Nikita Naumov, 2015-03-03
@aptnru

I tried, but half refused to work, because the script dynamically substitutes values, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question