0
const postSignUpDetails = async (event)=>{

    event.preventDefault();  
    const data = {
        firstName : document.getElementById('first_name').value,
        lastName : document.getElementById('last_name').value,
        email : document.getElementById('email').value,
        password : document.getElementById('password').value,

    };

    console.log(data);

   try {
        const result = await fetch('http://localhost:3000/api/v1/users/signup',{
            method:"POST",
            body: JSON.stringify(data),
            mode:'no-cors',
            headers: {
                "Content-Type": "application/json",
                // 'Content-Type': 'application/x-www-form-urlencoded',
            }
        });
        console.log(result);
   } catch (error) {
        console.log(error);
   }  
}

document.getElementById('sign_up').addEventListener('click',postSignUpDetails);

I am making a post request on my backend server. I am able to see body in request payLoad. But body on the backend is null. payLoad ScreenShot Backend Server Error

Why the request body not reaching on the backend

  • How does the backend code in Node.js look? Are you using Express? Have you remembered to register middleware like express.json() or bodyparser? – Thomas Frank May 02 '23 at 09:37
  • Well you've set your mode to `no-cors`, this will prevent the body from being read (see [Trying to use fetch and pass in mode: no-cors](https://stackoverflow.com/questions/43262121/trying-to-use-fetch-and-pass-in-mode-no-cors)). If you run into cors errors I'd suggest looking into [Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin'](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-receive-a-no-access-control-allow-origin-header-i) – Reyno May 02 '23 at 09:42
  • No-cors would work fine if the frontend is running on localhost:3000 as well, but I guess it isn't because then what's the use of an absolute url... In general setting a proxy to the backend api in Vite is a nice way to be able to work without CORS, and makes for easier deployment of the application later on. then you would only have to fetch from '/api/etc...' See the accepted answer for this question: https://stackoverflow.com/questions/64677212/how-to-configure-proxy-in-vite – Thomas Frank May 02 '23 at 10:29

0 Answers0