I am trying to get HTML data from any website using get API.
I have this URL: https://www.linkedin.com/
I am trying to get HTML page as text from this URL.
What I tried:
getData() {
const api = "https://www.linkedin.com/";
return this.httpClient.get(api, { responseType: "text" });
}
ngOnInit(): void {
this.getData().subscribe((value) => {
console.log(value);
});
}
I got this error:
Access to XMLHttpRequest at 'https://www.linkedin.com/' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
What did I miss?
You need to have proxy requests to enable CORS in your angular application. For doing this, simply create a proxy.config.json file and enter something like this.
{
"/api": {
"target": "http://localhost:5000",
"secure": false
}
}
You then need to inform angular about this configuration. You can do this by adding a proxyConfig option inside your angular.json file.
"architect": {
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "application-name:build",
"proxyConfig": "src/proxy.config.json"
},