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 ?
CodePudding user response:
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