How to add a contact to Dynamics CRM 365 with JavaScript

守給你的承諾、 提交于 2019-12-24 18:29:21

问题


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" }));

来源:https://stackoverflow.com/questions/51694272/how-to-add-a-contact-to-dynamics-crm-365-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!