Microsoft Dynamics CRM Web API似乎支持OData,并且支持添加新实体,例如使用JavaScript的联系人。我尝试使用以下代码访问API,但是有两个问题。一种是跨域资源共享,它不允许我的脚本执行,另一种是我得到拒绝访问的错误。
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" }));