当我使用这个 fetch 函数并在我的表单上测试它时,创建了一个只有一个 id 的新对象,我得到 201 状态而不是 200
export function PostData(userData){
let url = 'http://localhost:3001/users';
return new Promise((resolve, reject) => {
fetch(url,{
method: 'POST',
body: JSON.stringify(userData)
})
.then((response)=> response.json())
.then((responseJson) => {
resolve(responseJson);
})
.catch((error) => {
reject(error);
});
});
}
数据库.json
{
"users": [
{
"id": 1,
"email": "test@gmail.com",
"password": "12345"
},
{
"id": 2,
"email": "testtest@gmail.com",
"password": "123456"
},
{
"id": 3
}
]
}
这是登录功能
login(e){
e.preventDefault();
PostData(this.state).then((result) => {
let responseJSON = result;
console.log(responseJSON);
});
}
变化函数
onChange = async (e) =>{
this.setState({[e.target.name]: e.target.value} );
console.log(this.state);
}