[英]Req.body is Undefined while Post request through Fetch Api
When I try to call the POST Api through fetch method by passing the parameter in body then I got req.body undefined on backend side(Node js side).Also when I call the Same POST api through Postman by passing the parameter in body then它成功運行(我得到了 req.body)。 后端在 node js 中,前端在 React js 中。 任何幫助將不勝感激。
import React, { Component } from 'react'
import '../Css/Login.css'
export class Login extends Component {
constructor(props) {
super(props)
this.state = {
name: "",
password: ""
}
this.onChange = this.onChange.bind(this);
}
submitHandler = (e) => {
e.preventDefault();
console.log(this.state);
try {
let result = fetch('http://localhost:4000/users/login', {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: { name: this.state.name, password: this.state.password }
})
result.then((sucess) => { console.log(sucess) })
} catch (error) {
console.log(error)
}
}
onChange(e) {
this.setState({ [e.target.name]: e.target.value })
}
render() {
return (
<div>
<div className="LoginPage">
<div class="container Login_div">
<div className="Admin_panel"><h2>Admin Panel</h2></div>
<form onSubmit={this.submitHandler}>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" name="name" class="form-control" placeholder="Enter email" onChange={this.onChange} />
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" name="password" class="form-control" placeholder="Enter password" onChange={this.onChange} />
</div>
<button type="submit" class="btn btn-login">Submit</button>
</form>
</div>
</div>
</div>
)
}
}
export default Login
這是使用node-fetch
嗎? 所以我認為你需要將JSON.stringify()
添加到正文中。
try {
let result = fetch('http://localhost:4000/users/login', {
method: 'POST',
mode:'no-cors',
headers: {
'Content-Type':'application/json',
'Accept':'application/json'
},
body: JSON.stringify({ name: this.state.name, password: this.state.password })
});
result.then((success) => { console.log(success) });
} catch (error) {
console.log(error);
}
鏈接指的是Node Fetch-post 與 json 。 我希望它有效。
更新如果您的后端使用的是 ExpressJs,請確保您已添加。
const app = express();
// push them above the router middleware!
// parse application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// parse application/json
app.use(express.json());
您必須使用 JSON.stringify 將您的身體數據轉換為JSON.stringify
。
並確保在后端啟用cors
。
問題在於mode: 'no-cors'
,請刪除它。 因為application/json
不允許在no-cors
模式下使用。
請注意 mode: "no-cors" 只允許請求中的一組有限的標頭:
- 接受
- 接受語言
- Content-Language Content-Type,值為 application/x-www-form-urlencoded、multipart/form-data 或 text/plain
嘗試這個。
submitHandler = (e) => {
e.preventDefault();
console.log(this.state);
try {
let result = fetch('http://localhost:4000/users/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({ name: this.state.name, password: this.state.password })
})
result.then((sucess) => { console.log(sucess) })
} catch (error) {
console.log(error)
}
}
您還需要在構造函數中綁定 function submitHandler 以訪問“ this ”
this.submitHandler =this.submitHandler .bind(this);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.