web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Cors Issues Accessing API
Power Apps
Unanswered

Cors Issues Accessing API

(0) ShareShare
ReportReport
Posted on by 8

Hi,

 

I am facing issues in accessing an api from my PCF Control (calling the api from componentDidMount method). I am getting the below error message while calling the api. Surprisingly , this is not the error that occurs each time. Most of the time api call succeeeds and gets the data, but 20% of the api calls are failing with this error from the control. we are allowing ".dynamics.com" to send requests. Please note that this problem does not occur all the times, but pretty randomly some times ~20% times. 

 

Access to fetch at '*********' from origin '******' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I have the same question (0)
  • Hemant Gaur Profile Picture
    on at

    Is it specific to one browser ? I dont think this is realted to react as such but can be quick validated using a standard control . Can you check the browser console logs and network traffic to see if this is missing header and any difference in the failed and sucessful calls ? 

    Here are some other pointers which might help.

    Browser caching -  https://stackoverflow.com/questions/21104810/what-could-explain-the-browser-intermittently-not-loading-some-cors-crossorigin

    https://stackoverflow.com/questions/51128176/reactjs-api-data-fetching-cors-error

    Chrome extension can be used for testing CORS - https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

     

     

    hemant

     

     

  • PradeepKumarPeri Profile Picture
    8 on at

    Thanks for your help Hemant, it was not an issue with React i solved the problem at api. Many Thanks!


    @HemantG wrote:

    Is it specific to one browser ? I dont think this is realted to react as such but can be quick validated using a standard control . Can you check the browser console logs and network traffic to see if this is missing header and any difference in the failed and sucessful calls ? 

    Here are some other pointers which might help.

    Browser caching -  https://stackoverflow.com/questions/21104810/what-could-explain-the-browser-intermittently-not-loading-some-cors-crossorigin

    https://stackoverflow.com/questions/51128176/reactjs-api-data-fetching-cors-error

    Chrome extension can be used for testing CORS - https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

     

     

    hemant

     

     


     

  • Hemant Gaur Profile Picture
    on at

     


    @pradeepperi wrote:

    Thanks for your help Hemant, it was not an issue with React i solved the problem at api. Many Thanks!


    @HemantG wrote:

    Is it specific to one browser ? I dont think this is realted to react as such but can be quick validated using a standard control . Can you check the browser console logs and network traffic to see if this is missing header and any difference in the failed and sucessful calls ? 

    Here are some other pointers which might help.

    Browser caching -  https://stackoverflow.com/questions/21104810/what-could-explain-the-browser-intermittently-not-loading-some-cors-crossorigin

    https://stackoverflow.com/questions/51128176/reactjs-api-data-fetching-cors-error

    Chrome extension can be used for testing CORS - https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi

     

     

    hemant

     

     


     


    @pradeepperi , do you mind sharing some details on solution and sample code snippet? Might help someone in future 🙂

     

    hemant 

  • Mili12 Profile Picture
    7 on at

    @pradeepperi- Can you please share what was done to resolve this issue? I am facing a similar issue too.  Thanks in advance!

     

    -Mili

  • Hemant Gaur Profile Picture
    on at

    are you able to use the API just using simple html from different domain ? That setup can be used for debugging. I had pointed some resources in the thread which might be useful 

     

    Hemant

  • PradeepKumarPeri Profile Picture
    8 on at

    Thanks Hemant, i have restructured the api calling functionality to a custom action and calling the custom action from the control. So, it works correctly now. Thanks again!

  • Mili12 Profile Picture
    7 on at

    Pradeep, 

     

    Which library have you used to invoke the custom action from type script?  The standard way to invoke from JS is to use execute.online function but in type script what is the counterpart?  I failed when I tried to use the xrm-webapi-client library. Can you share the code at a high level for reference?  I am sure many others too have similar issue.

     

    -Mili

  • Ben Thompson Profile Picture
    1,400 on at

    This should show you what you need to do 

     

    refreshLicense(product:string) : Promise<Xrm.RetrieveMultipleResult>{
     'use strict';	
    		var clientUrl =(<any>this.context).page.getClientUrl();
    		var thisRef=this;
    		var req = new XMLHttpRequest();
    		req.open("POST", clientUrl + "/api/data/v9.1/hdn_ValidateLicense", true);
    		req.setRequestHeader("OData-MaxVersion", "4.0");
    		req.setRequestHeader("OData-Version", "4.0");
    		req.setRequestHeader("Accept", "application/json");
    		req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    		req.onreadystatechange = function() {
    			if (this.readyState === 4) {
    				req.onreadystatechange = null;
    				
    				if (this.status === 200) {
    					return this.response;
    
    					
    				} else {
    
    					return Promise.reject(error);
    				}
    			}
    		};
    		var body=	"{'ProductName': '"+product+"'}";
    		req.send(body);
    	}

     

  • a33ik Profile Picture
    3,306 Most Valuable Professional on at

    I found an alternative way of calling messages using execute of standard WebApi wrapper. Execute method is not exposed but it is available. Here is how I use it:

    const youractionrequest = {
    	getMetadata: function() {
    	return {
    		boundParameter: null,
    		parameterTypes: {},
    		operationType: 0,
    		operationName: "nameofyouractionhere"
    		};
    	}
    };
    
    (<any>context.webAPI).execute(youractionrequest).then((result: any) => {
    		result.json().then((response: any) => {
    			//use your parsed response here
    		});
    	}, 
    	(e: any) => {
    		//Handle reject here
    	});
  • Ben Thompson Profile Picture
    1,400 on at

    @a33ik The risk with that is that unless and until it's documented it could break at any point.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard