-3

In Angular 5, what do I need to consider regarding CORS, when accessing a remote resource via http.get()?

EXAMPLE:

let url = `http://www.google.com`;
this.http.get(url).subscribe(res => {
    console.log(res.text());
    // parse html
});

RESULT:

Failed to load https://www.google.com/: 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'null' is therefore not allowed access.
core.js:1350 ERROR 

Using the Angular and Firebase ecosystem, is there a recommended way to address this issue? Could I e.g. use a Cloud-function to access the remote url via Node.js? How do developers typically address this?

Ben
  • 15,938
  • 19
  • 92
  • 138
  • 1
    Some URLs will allow CORS requests, others will not. It seems the main google homepage does not. The only way to get around it would be to make the request from your own backend server – user184994 Jul 28 '18 at 16:51
  • Using google is just an example, but the actual url I am using returns the exact same error. Is there a known solution to *"make the request from your own backend server"* using the Angular and Firebase eco-systems? – Ben Jul 28 '18 at 16:57

1 Answers1

1

This is Cross-Origin Resource Sharing (CORS). Lean more about CORS.

Completely Unrelated with Angular.

Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell a browser to let a web application running at one origin (domain) have permission to access selected resources from a server at a different origin

Ref: MDN Docs for CORS

Ritwick Dey
  • 18,464
  • 3
  • 24
  • 37
  • Thank you for your answer. I am asking how to get around this to access the remote resource using the Angular and Firebase eco-systems. – Ben Jul 28 '18 at 16:54
  • Hi @Ben, This is not a problem. This a brower security. You can disable CORS security in your browser & this will work (Just for your own test purpose). But don't think others will do. – Ritwick Dey Jul 28 '18 at 16:57
  • 2
    Anther way is that, make a server and fetch a website from your server and then make your own api. :) @Ben – Ritwick Dey Jul 28 '18 at 16:59