简体   繁体   中英

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

Make changes in your request like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM