Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why react post to localhost not working? (mock API)

I’m going to use the fetch function to send a "post" using MOCK API. However, the post is not sent and 404 errors continue to appear.

Is it not possible to post on the localhost?

<WriteSubmitButton onClick={onDiaryPost}> Make ! </WriteSubmitButton>
const onDiaryPost = (e) => {
    e.preventDefault();
    fetch('http://localhost:3000/data/diaryList.json', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        date: write.date,
        title: write.title,
        text: write.text,
      }),
    })
      .then(res => res.json())
      .then(res => {
        if (res.success) {
          alert('add new something');
        }
      });
  };

why 404 not found ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can’t use POST on local json file. Instead of you can create a basic json-server on your local machine. It is very simple and usable. You only need to install it

npm install -g json-server

then start it

json-server --watch diaryList.json

For further information please check their documentation:
https://github.com/typicode/json-server

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading