Home > database >  VM126:1 Uncaught (in promise) SyntaxError: Unexpected token e in JSON at position 0 i think is about
VM126:1 Uncaught (in promise) SyntaxError: Unexpected token e in JSON at position 0 i think is about

Time:11-18

VM126:1 Uncaught (in promise) SyntaxError: Unexpected token e in JSON at position 0 i think is about my prevent defaut element

sorry for my bad english i not speak english very well

contactForm.addEventListener('submit', (e) => { e.preventDefault();

    let formData = {
        firstname: firstname.value,
        lastname: lastname.value,
        email: email.value,
        message: message.value
 
    }
    //  do fetch with request post of formData


    fetch('https://immo-serkas.herokuapp.com/contact', {
        method: 'POST',
        body: JSON.stringify(formData),
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
        }
 })
    .then(res => res.json())
    .then(data => {
        console.log("data : "   data)
        if (data) {
            // show success message
            alert("Email Sent");
            document.querySelector('.contact-form').style.display = 'none'
            document.querySelector('.contact-success').style.display = 'block'
        } else {
            // show error message
            document.querySelector('.contact-form').style.display = 'none'
            document.querySelector('.contact-error').style.display = 'block'
        }
    })
})

CodePudding user response:

This type of error occur when we parse incorrect json data.

example:-

let jsonData = '{
    "firstName":"harsh",
    lastName:"mangalam"
}'

JSON.parse(jsonData)

here lastName should be "lastName" to prevent from this type of error.

please check your incomming json response.

  • Related