将作品放在邮递员中,而不是AXIOS中 [英] Put works in Postman but not AXIOS

查看:18
本文介绍了将作品放在邮递员中,而不是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);
});

这篇关于将作品放在邮递员中,而不是AXIOS中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆