Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Getting CORS error while calling Dynamics 365 Operations JSON-Based custom service using Javascript

Posted on by Microsoft Employee

Hi,

I am developing a simple SPA application and trying to access a Dynamics 365 for Operations JSON-Based custom service. I am using ADAL.js library for authentication. The way for authentication i am following from This Dynamics Community thread.

But opportunity, after successfully getting the valid token, and calling my target api with acquired token, i am not able to call the API and getting CORS error.

PSB screenshot for the same

enter image description here

Below is my code sample:

    var req = new XMLHttpRequest()
   req.open("POST", organizationURI + "/api/services/ServiceGroup/Service/Operaton", true);

   //Set Bearer token
    req.setRequestHeader("Authorization", "Bearer " + token);
   req.setRequestHeader("Accept", "application/json");
   req.setRequestHeader("Content-Type", "application/json");

   req.onreadystatechange = function () {
    if (this.readyState == 4 /* complete */) {
     req.onreadystatechange = null;
     if (this.status == 200) {
      var empData = JSON.parse(this.response).value;
      console.log(empData);
     }
     else {
      var error = JSON.parse(this.response).error;
      console.log(error.message);
      errorMessage.textContent = error.message;
     }
    }
   };
   req.send();

*This post is locked for comments

  • Suggested answer
    Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Getting CORS error while calling Dynamics 365 Operations JSON-Based custom service using Javascript

    I will suggest you to post your question to related forum to get you answer quickly:community.dynamics.com/.../759

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting CORS error while calling Dynamics 365 Operations JSON-Based custom service using Javascript

    I am asking about Dynamics ERP  not for CRM.

  • gdas Profile Picture
    gdas 50,085 on at
    RE: Getting CORS error while calling Dynamics 365 Operations JSON-Based custom service using Javascript

    Yes from dynamics CRM 2016 onwards it's support cors . You don't need to enable it.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting CORS error while calling Dynamics 365 Operations JSON-Based custom service using Javascript

    Hi Goutam,

    Not talking about Microsoft Dynamics ERP not CRM.

    I have one more question, does CORS by default enable in Dynamics? if it doesn't enable by default then what is the way to enable it? –

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Getting CORS error while calling Dynamics 365 Operations JSON-Based custom service using Javascript

    Hi,

    I am not sure , may be you are missing something , could you please validate your step with below reference  -

    debajmecrm.com/.../dynamics-crm-cross-origin-resource-sharing-external-application-cors

    msdn.microsoft.com/.../mt595799.aspx

    Hope this helps.

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