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





what error you're getting on console "
– Sakhi Mansoor
Aug 30 at 15:50





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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế