[英]fetch on react component method with problem
Im trying to make an API CALL using fetch in a method (saveUser). 我试图使用方法(saveUser)中的访存进行API调用。 API is working fine and de method is doing his work well but I dont know why the PUT doesnt work.
API运作良好,de方法运作良好,但我不知道PUT为什么无法运作。
Here is the code: 这是代码:
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;
};
Here is all the code: manageUserPage 这是所有代码: manageUserPage
Make changes in your request
like this: 像这样在您的
request
更改:
fetch(url, {
method: 'PUT',
body: JSON.stringify(user),
headers: {
'Content-Type': 'application/json'
}
Let me know if the issue still persists and make sure you have enabled CORS. 让我知道问题是否仍然存在,并确保您已启用CORS。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.