0

I have an external API (which I can not control), which generates the Token. I tested it from .NET WEB API and able to get the token and everything is fine. But when I am trying to access same API endpoint (with Client ID, Client Secret, grant_type and scope) from my Angular 11 front end application, I am getting CORS error as below.

" Access to XMLHttpRequest at '' 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."

My Angular code to call API endpoint is :-

callToken()
  {
    var data="grant_type=client_credentials";
    var auth = btoa("ClientID:ClientSecret");
    var reqHeader=new HttpHeaders({
      'Authorization': 'Basic ' + auth,
      'Content-Type': 'application/x-www-form-urlencoded'
    });
    return this.http.post('API EndPoint', data,{headers: reqHeader});
  }

I have tried mutiple options but nothing is working. Same code is working fine in DOTNET.

Basu
  • 11
  • 1
  • Are you running your .NET API on localhost on the same port 4200 and it is working? – Frank Fajardo Jun 06 '21 at 01:00
  • Does your WEB API is CORS enabled with allow origins from `http://localhost:4200`? – Yong Shun Jun 06 '21 at 01:04
  • Yes I m running .NET API in local host. port number is different not 4200. But the the endpoint is allowing DOTNET API to get the token. But Angular getting CORS error. – Basu Jun 06 '21 at 01:30
  • I do not have any control on API which I am trying to access. But when i am accessing it from DOTENT it is working fine. CORS error coming when I am trying to access it from Angular. – Basu Jun 06 '21 at 01:32
  • Does this answer your question? [Angular - JSON Put and Delete Returns 403 (But postman app works well)](https://stackoverflow.com/questions/54168106/angular-json-put-and-delete-returns-403-but-postman-app-works-well) – deepakchethan Jun 06 '21 at 03:05
  • 1
    @Basu Check what are the header attribute goes in Request and Response headers in `DOTNET API` and your `http://localhost:4200` – Arunkumar Ramasamy Jun 06 '21 at 03:25

0 Answers0