0

当我使用这个 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);
    }
4

1 回答 1

1

状态 201 已创建 - 它表示成功的 POST 请求。状态 200 是 GET 请求的成功代码

于 2018-08-29T12:57:16.927 回答