Reputation: 2061
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?
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)
);
}
Upvotes: 1
Views: 19926
Reputation: 757
Add configuration in VSCode before run your application(F5)
"runtimeArgs": ["--disable-web-security"]
in launch.json file
Upvotes: -1
Reputation: 450
Modify your server to add the header Access-Control-Allow-Origin: *
to enable cross-origin requests from any domains.
Upvotes: -1
Reputation: 86790
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.
Upvotes: -1