I have this service:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
@Injectable()
export class LoginService {
constructor(private http: HttpClient) {}
getUsers() {
const options = {
headers: new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Headers': 'Origin, Content-Type',
'Content-Type': 'text/xml'
})
};
return this.http.get('myurl', options);
}
}
And I'm trying to access an API which has only GET
. This particular resource allows anonymous access.
However, I get the following errors (in Chrome console):
OPTIONS myurl 405 (Method Not Allowed)
Failed to load myurl: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 405.
Any ideas on how to fix these errors?