Angular 4 Access-Control-Allow-Origin error with localhost url
This means that, http://api.test/work.but
does not allow cross origin request. If this link is hosted by you, you will have to allow cross origin request. In your response headers, you will have to send: Access-Control-Allow-Origin: *
This means, any body request to your server.
If you want to allow access to specific domains, you can specify them.
Read more abouut CORS
here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
I was getting the same error; "Failed to load http://web-api_link: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.". I already had CORS installed on my chrome browser. It was on. I clicked on it and checked for "Enable cross-origin resource sharing". I disabled it, and enabled it again. It worked for me!
private callServiceGet(url: string, wC?: boolean): Promise<any> { return this.http.get<any>(url, { observe: 'response', withCredentials: wC }) .catch(err => [ err ]) .map(data => ({ status: data.status, data: data.body })) .toPromise(); }
with wC you have credentials to allow localhost too. It's simply a boolean, so set it to true if you want a complete access.
for example you could use it like this:
getJSON(): Promise<any> { return this.callServiceGet(`http://api.test/work`, true); }