我试图在方法(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
像这样在request
中进行更改:
fetch(url, {
method: 'PUT',
body: JSON.stringify(user),
headers: {
'Content-Type': 'application/json'
}
如果问题仍然存在,请确认您已启用CORS。