-->

How to add a contact to Dynamics CRM 365 with Java

2019-08-27 16:17发布

问题:

It seems that Microsoft Dynamics CRM Web API supports OData and it supports adding new entities e.g. contacts using JavaScript. I tried accessing the API with the following code, but I have two problems. One is cross-origin resource sharing which does not allow my script to execute and the other is that I get access-denied error.

var req = new XMLHttpRequest()
req.open("POST",encodeURI(clientURL + "/api/data/v8.1/accounts"), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
 if (this.readyState == 4 /* complete */) {
  req.onreadystatechange = null;
  if (this.status == 204) {
   var accountUri = this.getResponseHeader("OData-EntityId");
   console.log("Created account with URI: "+ accountUri)
  }
  else {
   var error = JSON.parse(this.response).error;
   console.log(error.message);
  }
 }
};
req.send(JSON.stringify({ name: "Sample account" }));