【问题标题】:fetch on react component method with problem获取有问题的反应组件方法
【发布时间】:2019-02-05 15:11:43
【问题描述】:

我正在尝试在方法 (saveUser) 中使用 fetch 进行 API CALL。 API 运行良好,de 方法运行良好,但我不知道为什么 PUT 不起作用。

代码如下:

saveUser(user: IUser) {
  user = this.state.user;
  let userId = user._id;
  let url = `http://localhost:4200/api/update-user/${userId}`;

  fetch(url, {
             method: 'put',
             body: JSON.stringify(user),
             headers: {
               'Content-Type': 'application/json'
             }
       }).then(res => res.json())
       .catch(error => console.error('Error:', error))
       .then(response => console.log('Success:', response));
       debugger;
    };

这里是所有代码:manageUserPage

【问题讨论】:

  • 你在控制台上遇到了什么错误“
  • 你当然用邮递员和put方法测试过API?
  • 感谢大家的回复!是的,它可以在邮递员上正确使用并使用 de JSON。
  • 我没有在控制台上显示任何内容。当我使用邮递员时,它显示用户已正确修改。这就是为什么我认为问题出在方法上。
  • 您得到的响应是什么错误代码?

标签: javascript node.js reactjs mongoose


【解决方案1】:

像这样在您的request 中进行更改:

fetch(url, {
             method: 'PUT',
             body: JSON.stringify(user),
             headers: {
               'Content-Type': 'application/json'
             }

如果问题仍然存在,请告诉我,并确保您已启用 CORS。

【讨论】:

  • 大写
  • 我用其余的代码更新了帖子。再次感谢你的帮助。 :)
猜你喜欢
  • 2021-07-27
  • 1970-01-01
  • 2019-04-24
  • 2018-10-05
  • 2021-07-31
  • 2020-11-15
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多