不能放"身体'在获取发布请求

时间:2018-03-13 12:39:28

标签: javascript json api post fetch

我在发布数据抛出REST API和获取请求时遇到问题。请求状态为200,对象转到数据库,但没有正文:(因为我使用Insomnia,对象只有一个id和一些" __ v" key。这里的结果是数据库

    {
        "_id": "5aa7c133610f563a089d9ccf",
        "__v": 0
    },
    {
        "_id": "5aa7c134610f563a089d9cd0",
        "__v": 0
    },

和我的API函数

function api(endpoint) {
        endpoint = `http://localhost:3030/api/${endpoint}`;
        return {
            get() {
                return fetch(endpoint)
            },
            post(body) {
                return fetch(endpoint, {
                    method: 'POST',
                    headers: {'Content-Type':'application/x-www-form-urlencoded'},
                    body: JSON.stringify(body)
                });
            },
            delete() {
                return fetch(endpoint, {
                    method: 'DELETE'
                })
            }
        }
    }

    let obj = {
       name: "Apple",
       nutritional_value: 100,
       proteins: 200,
       fats: 120,
       carbohydrates: 3215,
       description: "some delicious apple",
       image: "apple.png",
       type: "fruit"
    };

    api('food').post(obj);

那么,问题是什么?请帮助:)

0 个答案:

没有答案
相关问题