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 unable to call the API and getting CORS error.
PSB screenshot for the same
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();