英文:
Json-server not storing fields properly
问题
以下是您提供的代码的中文翻译:
我正在使用一些React代码并尝试使用json-server。我编写了这段代码来实现一些简单的逻辑,以创建带有标题的笔记并将其存储在数据库中。为了模拟REST API,我使用了json-server。但对我来说有些地方似乎不起作用。当我通过curl或httpie发布新的笔记时,它起作用。但当我在我的应用程序中使用下面的表单时,它添加了新的笔记,但笔记是空的(只有id),没有内容和标题。我漏掉了什么?
这是代码:
import { useState } from "react";
export default function () {
const [title, setTitle] = useState('title');
const [content, setContent] = useState('content here');
const [errorMessage, setErrorMessage] = useState('');
let handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
try {
let res = await fetch("http://localhost:3001/notes", {
method: "post",
body: JSON.stringify({
title: title,
content: content,
})
})
let resJson = await res.json();
console.log(resJson);
if (res.status == 201) {
setTitle('')
setContent('')
} else {
setErrorMessage(`something went wrong, ${res.status}`)
}
} catch(err) {
console.log(err);
}
}
return (
<form onSubmit={handleSubmit}>
<div className="flex flex-col">
<input
type="text"
value={title}
onChange={e => setTitle(e.target.value)}
className="border-2 border-gray"/>
<textarea
className="border-2 border-gray"
value={content}
onChange={(event) => setContent(event.target.value)}
autoFocus rows={5} wrap="soft"/>
<button type="submit">Add</button>
{errorMessage && <p>{errorMessage}</p>}
</div>
</form>
);
}
我像这样运行json-server:
json-server --watch db.json --port 3001
curl(http)命令可以正常工作:
http post localhost:3001/notes --raw '{ "title": "A", "content": "B" }'
或:
http post localhost:3001/notes title="test" content="test content"
因为当我显示所有笔记时,我可以看到发送的内容:
[
{
"content": "test content",
"id": 11,
"title": "test"
},
{
"content": "B",
"id": 15,
"title": "A"
}
]
但当我使用我的应用程序时,添加到数据库的仅仅是:
{
"id": 16
}
这里漏掉了什么?
英文:
I am playing with some react code and trying out json-server.
I wrote this code to have some simple logic to create notes with titles and store it in db. To mimic rest api I used json-server. But something doesn't work for me. When I post a new note through curl or httpie it works. But when I use this form in my app below, it adds the new note but the note is empty (it has only id), without content and title. What am I missing here?
The code is:
import { useState } from "react";
export default function () {
const [title, setTitle] = useState('title');
const [content, setContent] = useState('content here');
const [errorMessage, setErrorMessage] = useState('');
let handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
try {
let res = await fetch("http://localhost:3001/notes", {
method: "post",
body: JSON.stringify({
title: title,
content: content,
})
})
let resJson = await res.json();
console.log(resJson);
if (res.status == 201) {
setTitle('')
setContent('')
} else {
setErrorMessage(`something went wrong, ${res.status}`)
}
} catch(err) {
console.log(err);
}
}
return (
<form onSubmit={handleSubmit}>
<div className="flex flex-col">
<input
type="text"
value={title}
onChange={e => setTitle(e.target.value)}
className="border-2 border-gray"/>
<textarea
className="border-2 border-gray"
value={content}
onChange={(event) => setContent(event.target.value)}
autoFocus rows={5} wrap="soft"/>
<button type="submit">Add</button>
{errorMessage && <p>{errorMessage}</p>}
</div>
</form>
);
}
I run json-server like this:
json-server --watch db.json --port 3001
The curl (http) command works fine:
http post localhost:3001/notes --raw '{ "title": "A", "content": "B" }'
Or:
http post localhost:3001/notes title="test" content="test content"
Because when I display all notes I can see the content sent there:
http localhost:3001/notes
[
{
"content": "test content",
"id": 11,
"title": "test"
},
{
"content": "B",
"id": 15,
"title": "A"
}
]
But when I use my app, all that is added to the db is:
{
"id": 16
}
What is missing here?
答案1
得分: 1
"Maybe you are missing the headers
in your fetch request, which is necessary to specify the content type of the data you're sending. You need to set the Content-Type
header to application/json
for the server to correctly understand the request payload."
英文:
Maybe you are missing the headers
in your fetch request, which is necessary to specify the content type of the data you're sending. You need to set the Content-Type
header to application/json
for the server to correctly understand the request payload.
let res = await fetch("http://localhost:3001/notes", {
method: "post",
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: title,
content: content,
})
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论