Why do I get an error when I send data to the server?

  Kiến thức lập trình

Why do I get an error when I send data to the server?

I’m creating a form to login to a site, but I’m getting an error saying the JSON is invalid.

Output upon sending:

Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

Code:

const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault()
    console.clear()
    const url_api = 'http://localhost:5555/api'
    const formData = new FormData(event.currentTarget)
    const res = await fetch(`${url_api}/auth/login`, {
      method: 'POST',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        email: formData.get('email'),
        password: formData.get('password')
      })
    })
    const result = await res.json()
    console.log(result)
}

I checked everything and found nothing.

New contributor

yuriyProgg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT