2

I have a problem that I can't seem to figure out. I want to send a http params request from my Angular client to server using below code but I am getting exception:

http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass

I want clarity about it, did I make any mistakes in Angular or is this a server side problem?

Auth:

login(username: string, password: string) {
    let params = new HttpParams();
    params = params.append('email', username);
    params = params.append('password', password);
    return this.http.post<any>('URL',{params:params})
      .pipe(map(user => {
        if (user && user.token) {
        }
      }),
        catchError(this.handleError)
      );
  }
halfer
  • 18,701
  • 13
  • 79
  • 158
AbhiRam
  • 1,833
  • 5
  • 34
  • 74
  • Possible duplicate of [Response to preflight request doesn't pass access control check](https://stackoverflow.com/questions/35588699/response-to-preflight-request-doesnt-pass-access-control-check) – JJJ Dec 12 '18 at 06:22
  • @AbhiRam Check this: https://stackoverflow.com/questions/53087341/the-access-control-allow-origin-header-has-a-value-http-localhost4200-tha/53683422#53683422 – Saddam Pojee Dec 12 '18 at 09:44

3 Answers3

0

Yes, This is server-side problem not client side.

You need to enable CORS for your localhost from server side in order to consume API's from your Angular code.

PS: For a quick fix you can install chrome plugin to enable CORS but this is not recommended approach ever.

Pardeep Jain
  • 71,130
  • 29
  • 141
  • 199
0

Modify your server to add the header Access-Control-Allow-Origin: * to enable cross-origin requests from any domains.

Farhad Lavaei
  • 432
  • 4
  • 7
0

Add configuration in VSCode before run your application(F5)

"runtimeArgs": ["--disable-web-security"]

in launch.json file

Dane Brouwer
  • 1,974
  • 1
  • 17
  • 21
abinthomas12914
  • 569
  • 4
  • 7