-3

I can't get my Angular App working to send a http request to my locally running laravel-api. In Postman it's working perfectly, but I can't get it working in Angular.

This is the working post-request triggered by Postman. Postman POST-Request - works

This is my Angular-logic:

login(username: string, password: string): Observable<any> {
    let body = new FormData();
    body.append('email', username);
    body.append('password', password);
    this.http.post(this.LOGIN_USER_URL, body);

    return this.http.post<any>(this.LOGIN_USER_URL, body);
  }

If I run my angular application and the post-requests get triggered, it's returning nothing: enter image description here

Does anybody know, what's wrong with my code?

Jul
  • 79
  • 1
  • 1
  • 9
  • Your angular code snippet has the post request being made twice it seems. Also you need to set X-CSRF token for any form/post request with a csrf token - don't know how it's done in angular setup – Donkarnash Dec 05 '20 at 20:50

1 Answers1

2

The problem was, that CORS wasn't enabled in my laravel-api. This is the solution, that worked for me: https://stackoverflow.com/a/34895806/8349707

I solved it by adding the following lines to my index.php file:

header('Access-Control-Allow-Origin: *');  
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
Jul
  • 79
  • 1
  • 1
  • 9