axios仅发布保存ID和未添加的其他数据

时间:2019-01-30 09:04:20

标签: axios

我正在使用json服务器。当我使用邮递员或我的应用程序时,我可以看到该帖子成功完成。但是,仅记录id,而没有其他记录。

为什么会这样? 例如,如果我这样做 http://localhost:3001/contacts/?id=48&firstname=rrtty&lastname=yryrty&company=trytryrt&email=tyrtyr&message=tyrtytry

只会创建一个ID,而不会插入我的ID。

{
 "contacts": [
    {
      "id": 1,
      "firstname": "Robert",
      "lastname": "Swartez",
      "company": "new company",
      "email": "rob2eeeee3@gmail.com",
      "message": "hello world!"
    },
    {
      "id": 2,
      "firstname": "Lreucy",
      "lastname": "Bagggllmer",
      "company": "new company",
      "email": "lucreybee5e6@gmail.com",
      "message": "does this work!"
    },
    {
      "id": 3
    },
    {
      "id": 4
    },
    {
      "id": 5
    },
    {
      "id": 6
    },
    {
      "id": 7
    },
    {
      "id": 8
    },
    {
      "id": 9
    },
    {
      "id": 10
    },
    {
      "id": 11
    },
    {
      "id": 12
    },
    {
      "id": 13
    },
    {
      "id": 14
    },
    {
      "id": 15
    }
  ]
}

仅添加ID,也不添加其他数据。

请注意,即使使用邮递员,我也只能看到添加的id,而看不到参数

2 个答案:

答案 0 :(得分:1)

尝试将您的方法签名更改为此:

import axios from 'axios';

export default function callApi(method, url, path, data) {   
  switch(method){
    case 'create':
    {
      alert(`${url}`+ {data})
      return axios.post(`${url}`,{data})  
      .catch(error => {
        throw(error.response);
      });
    }
    case 'getOne':{
      return axios.get(`${url}`+`${path}`+`${data}`)  
      .catch(error => {
        throw(error);
      });
    }

    default:
    {
      return axios.get(`${url}`+`${path}`)  
      .catch(error => {
        throw(error);
      });
    }

  }

};

JS不是一种类型化的语言,默认情况下所有内容都可以为空。但是,如果需要,您可以提供默认值... export default function callApi(method = '', url = '', path = '', data = {})

答案 1 :(得分:0)

我终于在

上找到了解决方案

https://github.com/typicode/json-server/issues/711

确保将内容类型请求标头设置为application / json