将作品放在邮递员中,而不是AXIOS中

这是我MERN应用程序中最奇怪的事情。当我从Postman向我的API进行PUT时,它可以工作,并更新我的API和MongoDB。在前端,它不会更新API,即使控制台日志显示的值是正确的,URL是相同的?如有任何帮助或指示,我们将不胜感激。已经玩了好几天了。

邮递员证明更新工作

我的axios代码如下:

handlePlayerSubmit(id, player) {
    console.log('This is the id: ' + id);
    console.log('This is the player: ' + player);

    let apiUrl = 'http://localhost:3001/api/teams';
    //sends the id and new author/text to our api
    axios.put(`${apiUrl}/${id}`, player).catch(err => {
      console.log(err);
    });

}

所以我知道它正在触发,因为控制台日志...不确定为什么不更新API?

它也不是控制台。正在记录错误。

DEV工具中的网络屏幕截图

来自网络选项卡的标头:

JSON

这是因为Axios将推荐答案对象序列化为JSON。要以应用程序/x-www-form-urlencode格式进行序列化,您需要使用techniques described in the Axios documentation之一。

我认为qs对您来说是个不错的解决方案:

let apiUrl = 'http://localhost:3001/api/teams';
//sends the id and new author/text to our api
axios.put(`${apiUrl}/${id}`, qs.stringify(player)).catch(err => {
  console.log(err);
});

相关文章