1

I have tried to refer a project Angular Reference Project Link from MSFT and now i am trying to call a rest API from Angular and i am trying to create a singleton service, but whenever i try to run the application with Singleton the application fails.

Also i cannot use any Angular-CLI commanda as there is no angular-cli.json file present in the Yeomen solution for Angular. How can i work with it?

Aagam Doshi
  • 155
  • 2
  • 14
  • I think we need more info to help you. I just went through this exercise, so it's fresh in my mind. What errors are you observing? How are you calling the REST api? Update your question and I might be able to offer some insights. – Andrew Apr 28 '20 at 01:33
  • @Andrew thanks for reverting , i am trying to call a rest API with Http, but it failed saying cannot server http over https as its mentioned in official Microsoft Docs that you must use https calls only. So while making https calls to my server, i am getting CORS issue, and i cant change the server code to add new headers in response , is there any other way where we can achieve the same from Node or Angular – Aagam Doshi Apr 29 '20 at 06:35
  • Awaiting for updates @Andrew – Aagam Doshi Apr 30 '20 at 05:58
  • Again, it would be helpful to see the offending line of code, and the http request and responses you're getting. But off the top of my head, if you don't have access to the server and it forbids CORS, you're limited in your options. You might have some luck with JSONP - https://stackoverflow.com/questions/2067472/what-is-jsonp-and-why-was-it-created – Andrew Apr 30 '20 at 23:12

1 Answers1

1

Solving CORS errors without having access to the destination server forces you into a few options.

  1. Proxy server - create a server you DO control with Access-Control-Allow-Origin: * and make the request from your angular app to that proxy server. The proxy server will then forward to the destination server.

  2. JSONP - see: What is JSONP, and why was it created? . This bypasses the CORS restriction as you are allowed to load remote scripts. The result of the load can be the result of the API call. You'll just have to parse it to your liking.

Andrew
  • 392
  • 2
  • 12