search

Home  >  Q&A  >  body text

javascript - Using json-server POST data results only have id

Use json-server as the data source

fetch('http://localhost:3000/user', {
            method: 'POST',
            body: JSON.stringify({name:name.value, age:age.value, gender:gender.value}),
                headers: {
                    'Cotent-Type': 'application/json'
                }
            })
            .then(res => res.json())
            .then(res => {
                if (res.id) {
                    alert('添加用户成功');
                    this.setState({name: '', age: 0, gender: ''});
                } else {
                    alert('添加失败');
                }
            })
            .catch(err => console.error(err));

As a result, only one

was added
  {
    "id": 10002
  }

Two requests were sent in the network, one was an OPTION request. What is the purpose of this request?
The second request is a post request, which contains the data to be added, but there is only one ID information in the json file. Solution

typechotypecho2729 days ago862

reply all(1)I'll reply

  • 欧阳克

    欧阳克2017-06-12 09:26:07

    The two requests sent by the

    fetch method are

    The first post used to be the method in the method. For example, you can change method: 'POST' to method: 'PUT'. The post used to be the configuration information of PUT

    The second one is to send a request. Does the json file only have id information? Is it a background setting problem?

    reply
    0
  • Cancelreply