Problem with POST at Angular

Asked

Viewed 34 times

0

I’m building an Angular login system. The back-end is in cakephp. The request for preflight is being done correctly and receiving status 200 (figure below). After the preflight, the angle sends the empty POST request, without the login parameters. If I keep repeating the request, sometimes it sends the correct request. My job is the following::

SignIn(email = "", password = ""): Observable<Response> {
  const api = environment.apiURL + "login";
  const body = { email, password };
  const options = { headers: this.headers };
  return this.http.post<any>(api, body, options).pipe(
    take(1),
    tap(
      (success) => this.processLogin(success)
      error => console.error(error)
    )
  );}

On the console he tells me that it was a problem with CORS, but in cakephp it is enabled and as sometimes the request works correctly, I believe the problem is with the angular. Does anyone have any idea what I’m doing wrong?

1 answer

0

I solved the problem by modifying my header a little. Before I used to

headers = new HttpHeaders({
Accept: "application/json",
"Content-type": "application/json; charset=utf-8",

When I modified it to:

headers = new HttpHeaders({
Accept: "application/json ; text/plain",
"Content-type": "application/json; charset=utf-8",

Then it started working 100%. If anyone knows why it worked and wants to explain to us, they are welcome.

Browser other questions tagged

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