I have to get data from a json hosted on an external server online. The url look like http://example.com/path/to/resource.json
. I can read this json via browser, postman and some other tool.
But when I try to get the data from an Angular 11 application I get the CORS error:
Access to XMLHttpRequest at 'http://example.com/path/to/resource.json'
from origin 'http://localhost:4200' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
The code is literally just the GET with the HttpClient
class:
this.http.get('http://example.com/path/to/resource.json)'
What I tried was to configure the proxy but without success:
// proxy.conf.json
{
"/path": {
"target": "http://example.com",
"secure": false
}
}
// angular.json
"serve": {
[...]
"options": {
[...]
"proxyConfig": "proxy.conf.json"
},
[...]
},
Both files are in the root folder of the project.
I don't have control on the server, so I cannot allow CORS on it or change other settings but I don't think this is the issue.
I don't understand why I can access resources via many tools but I always have problems to do the same with Angular.