I am developing an application using Angular 2 and I find the following error when I try to make an API call:
XMLHttpRequest cannot load http://localhost:9090/conf. 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:5555' is therefore not allowed access.
Here, there are two examples of my calls:
import { Http, Response } from '@angular/http';
// ...
@Injectable()
export class ConfService {
// ...
getConf(): Observable<string[]> {
return this.http.get(
'http:localhost:9090/conf?id=1',
{
headers: {
'Accept': 'application/json;charset=UTF-8'
}
}
)
.map((res: Response) => res.json())
.catch(this.handleError);
}
setConf(conf: any[]): Observable<string[]> {
return this.http.get(
'http:localhost:9090/conf?id=1',
conf,
{
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}
)
.map((res: Response) => res.json())
.catch(this.handleError);
}
}
However, when I use cURL it works!
$ curl -XPUT 'localhost:9090/configuration?id=1' \
> -H 'Content-Type: application/json'
> -d '{"conf":1234}'
Success!
$ curl -XGET 'localhost:9090/conf?id=1'
{"conf":1234}
What is happening here?
Thanks