fetch on react component method with problem
fetch on react component method with problem
Im trying to make an API CALL using fetch in a method (saveUser). API is working fine and de method is doing his work well but I dont know why the PUT doesnt work.
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
of course you tested API with postman and put method ?
– xadm
Aug 30 at 15:53
Thanks everyone for the response! Yes, it works on postman correctly and with de JSON.
– lockaos
Aug 30 at 15:55
I doesn't shows anything on console. When I use postman it shows the user modified correctly. Thats why i think that the problem is on the method.
– lockaos
Aug 30 at 15:58
What error code you're getting in response ?
– Sakhi Mansoor
Aug 30 at 16:06
1 Answer
1
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.
I already have PUT :S
– lockaos
Aug 30 at 16:17
Make it uppercase
– Sakhi Mansoor
Aug 30 at 16:18
and see the error code ?
– Sakhi Mansoor
Aug 30 at 16:18
I updated the post with the rest of the code. Thanks again for all your help. :)
– lockaos
Aug 30 at 16:24
see my edited answer
– Sakhi Mansoor
Aug 30 at 16:41
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
what error you're getting on console "
– Sakhi Mansoor
Aug 30 at 15:50