我收到一个空的 json 对象
发布到路线时我收到一个空的 JSON 对象 /api/register
带有一个 JSON 对象,
{
"username":"test2",
"password":"1234567"
}
我正在尝试 console.log req.body
const UserModel = require("./Server/models/User")
const Post = require("./Server/models/Post")
const bodyParser = require("body-parser")
const express = require("express")
const app = express()
app.use(express.json())
app.get("/",(req,res)=> {
res.send("Home")
})
app.get("/api/register",(req,res)=> {
res.send("register")
})
app.post("/api/register",(req,res)=> {
console.log("recieved")
console.log(req.body)
})
app.listen(3001,()=> {
console.log("listening on port 3001")
})
前端
import React, { useState } from 'react';
import './App.css';
function App() {
const [username,setUsername] = useState("")
const [password,setPassword] = useState("")
function handleSubmit(event) {
event.preventDefault
fetch("http://localhost:3001/api/register",{
method:"post",
headers:{
"Content-Type" : "application/json"
},
body:JSON.stringify({username:username,password:password})
})
}
return (
<div className="App">
<form onSubmit={handleSubmit}>
<input placeholder="username" value={username} onChange={ e => setUsername(e.target.value)} />
<input placeholder="password " value={password} onChange={ e => setPassword(e.target.value)}/>
<button type='submit' >Submit</button>
</form>
</div>
);
}
export default App;
I'm receiving an empty JSON object when posting to the route/api/register
with a JSON object of
{
"username":"test2",
"password":"1234567"
}
I'm trying to console.log the req.body
const UserModel = require("./Server/models/User")
const Post = require("./Server/models/Post")
const bodyParser = require("body-parser")
const express = require("express")
const app = express()
app.use(express.json())
app.get("/",(req,res)=> {
res.send("Home")
})
app.get("/api/register",(req,res)=> {
res.send("register")
})
app.post("/api/register",(req,res)=> {
console.log("recieved")
console.log(req.body)
})
app.listen(3001,()=> {
console.log("listening on port 3001")
})
Front-end
import React, { useState } from 'react';
import './App.css';
function App() {
const [username,setUsername] = useState("")
const [password,setPassword] = useState("")
function handleSubmit(event) {
event.preventDefault
fetch("http://localhost:3001/api/register",{
method:"post",
headers:{
"Content-Type" : "application/json"
},
body:JSON.stringify({username:username,password:password})
})
}
return (
<div className="App">
<form onSubmit={handleSubmit}>
<input placeholder="username" value={username} onChange={ e => setUsername(e.target.value)} />
<input placeholder="password " value={password} onChange={ e => setPassword(e.target.value)}/>
<button type='submit' >Submit</button>
</form>
</div>
);
}
export default App;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不会从邮政端点发回任何内容。你应该这样做。
在这种情况下,我们发回用户发送的数据。
You are not sending back anything from post endpoint. You should do it like this.
In this case we send back the data that user sends.
看来您没有从 API 发送任何内容。您可以使用 .send() 函数将数据发送回调用者。
另外,请编辑问题并指定您期望在哪里打印 json(控制台、网页等)
Seems like you are not sending anything from your API. You can use .send() function to send data back to the caller.
Also, please edit the question and specify where do you expect json printing (console, webpage etc.)