这是我的 MERN 应用程序中最奇怪的事情。当我从 Postman 向我的 api 执行 PUT 时,它会工作并更新我的 api 和 mongoDB。在前端,即使控制台日志显示正确的值并且 url 相同,它也不会更新 api?任何帮助或指导将不胜感激......已经玩了好几天了。
邮递员证明更新工作
我的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?
而且它不是 console.logging 错误。
开发工具中的网络屏幕截图
网络选项卡中的标头:
发生这种情况是因为 Axios 将 JavaScript 对象序列化为 JSON。要以 application/x-www-form-urlencoded 格式进行序列化,您将需要使用其中之一Axios 文档中描述的技术 https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format.
I think qs https://github.com/ljharb/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);
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)