PUT method does not work like the others in ANGULAR

Asked

Viewed 41 times

0

I created a crud application where reading, reading by id, creation and deletion works well. The PUT method does not work, returns CORS error inserir a descrição da imagem aqui

I have done several console.log to see if the object is being passed correctly to the update function and is coming in the correct way (the change made). However, it does not update the record.

The requisition code follows below

create(colaborador: Colaborador): Observable<Colaborador> {
    return this.http.post<Colaborador>(environment.apiUrl, colaborador).pipe(map((obj) => obj), catchError(e => this.errorHandler(e)))
  }
  read(): Observable<Colaborador[]> {
    return this.http.get<Colaborador[]>(environment.apiUrl)
  }
  readById(_id: string | null): Observable<Colaborador> {
    const url = `${environment.apiUrl}/${_id}`
    return this.http.get<Colaborador>(url).pipe(map((obj) => obj), catchError(e => this.errorHandler(e)))
  }
  update(colaborador: Colaborador): Observable<Colaborador>{
    const url = `${environment.apiUrl}/${colaborador._id}`
    console.log(colaborador._id)
    console.log(colaborador);
    return this.http.put<Colaborador>(url, colaborador).pipe(retry(2), catchError(e => this.errorHandler(e)))
  }
  delete(_id:number): Observable<Colaborador>{
    const url = `${environment.apiUrl}/${_id}`
    return this.http.delete<Colaborador>(url).pipe(map((obj) => obj), catchError(e => this.errorHandler(e)))
  }

I’m using the crudcrud testing API. I tried with pipe(map according to the other methods, but same result. It would be necessary to send some header?

  • This is CORS error, would have to enable the origin of who is trying to do the post, this makes the back-end!

  • So. But this back I do not have access and in the documentation it accepts put. And the other methods work well

  • Probably some item is missing from your header that you need to put in. Check the API documentation.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.