json - 为什么我的 POST 请求不会更新我的 data.json 文件?
问题描述
因此,用户应该能够填写表单并提交,这应该使用新评论更新本地 JSON 服务器。目前它不工作。我正在使用我使用 json-server --watch db.json 检查的正确端口。我做错了什么?
class CommentForm extends Component {
handleSubmit = (e) => {
e.preventDefault();
console.log("submitted");
const body = e.target.body.value;
fetch("http://localhost:3000/comments", {
method: "POST",
body: JSON.stringify(body)
}).then((response) => {
console.log(response);
return response.json(); // do something with response JSON
});
};
render() {
return (
<div>
<h1>What is your favorite breed?</h1>
<form onSubmit={this.handleSubmit}>
<label>Answer: </label>
<br />
<textarea type="text" name="body" />
<br />
<input type="submit" />
</form>
</div>
);
}
}
export default CommentForm
{
"comments": [
{
"text": "Australian Shepherd!"
},
{
"text": "German Shepherd!"
},
{
"text": "Mutts all the way"
},
{
"text": "Chihuahua"
},
{
"text": "Great Pyrenees rock!!!"
}
]
}
解决方案
推荐阅读
- r - 如何在 R 的 Highchart 图例中添加换行符?
- elasticsearch - 基于条件的弹性搜索查询(if else 或 Union case)
- reactjs - React fetch 无法在 ip 上联系 api
- reactjs - 如何访问/提取我的反应应用程序当前呈现的 GeoJSON?
- firebase - 防止我的文本输入在我的聊天应用程序中发送空值
- javascript - Axios 和 Flask - 在请求之间保存会话 cookie
- php - 拉拉维尔。尝试使用 eloquent 从关系中获取列的值时出错
- drupal - 运行drupal ms:错误:在include()中找不到类'Symfony\Contracts\EventDispatcher\Event'
- tensorflow - 两个正态分布的重叠概率 tfp.distributions.Normal()
- list - 如何从数据框中的列创建稀疏矩阵?