Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

MS CRM Web API blocked by CORS policy

Posted on by Microsoft Employee

Hi Experts,

I am facing API blocked by CORS policy error when i trying to access data from my custom WEP API.

below code i am using for call wep API call.

var orgurl = "<web api url>";
var req = new XMLHttpRequest();
req.open("GET", encodeURI(orgurl), false);
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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.setRequestHeader('Access-Control-Allow-Credentials', 'false');
req.setRequestHeader('Access-Control-Allow-Origin', '*');
req.setRequestHeader('Access-Control-Allow-Methods', '*');
req.setRequestHeader('Access-Control-Allow-Headers', "*");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
console.log("this.status: " + this.status);
if (this.status === 200) {
console.log("this.response: " + this.response);
var result = JSON.parse(this.response);
if (result.length > 0) {
for (var i = 0; i < result.length; i++) {
var obj = {};
obj.name = result[i].NAME;
console.log("obj: "+obj);
}
}
}
} else {
alert(this.statusText);
}
}
};
req.send();

below is error message:

Access to XMLHttpRequest at '<Web Api Url>' from origin 'https://<Org>.dynamics.com' 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.

Above code is working fine if i am using chrome after disable-web-security.

using command  "chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security"

i have added CROS Origin allow settings in Web API C# config and controller files as well, still i am unable to get response from my Web API.

Please provide your suggestions. 

  • Suggested answer
    bravoreddy Profile Picture
    bravoreddy 67 on at
    RE: MS CRM Web API blocked by CORS policy

    When I had this issue and we realized this the vpn addresses has to be whitelisted for this api url for your CRM server.

  • priyatham Profile Picture
    priyatham 15 on at
    RE: MS CRM Web API blocked by CORS policy

    Hi,

    Do you still have this issue?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans