使用Odata Rest服务创建案例记录时出错

时间:2014-08-07 09:56:37

标签: javascript rest dynamics-crm-2011 odata dynamics-crm

我在CustomEntity上有Ribbon按钮。 CustomEntity有一个文本字段和帐户查找基于我正在创建案例记录的字段。但记录没有创造。有人可以帮我吗?我做错了。以下是我的样本

function test() {
debugger;
var PostID = Xrm.Page.getAttribute("ism_name").getValue();
var ismaccount = Xrm.Page.getAttribute("ism_account").getValue()[0].name;
var serverUrl = document.location.protocol + "//" + document.location.host;
var incident = {};
incident.Title = PostID; 
incident.CustomerId = ismaccount;
var jsonEntity = window.JSON.stringify(incident);
//incident.Description = "Dynamicallty created record using REST Odata";
var oDataPath = serverUrl + "/Retail/XRMServices/2011/OrganizationData.svc"; 
var retrieveReq = new XMLHttpRequest(); 
var Odata = oDataPath + "/IncidentSet";
retrieveReq.open("POST", Odata, true);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
retrieveReq.setRequestHeader("X-HTTP-Method", "CREATE");
retrieveReq.send(JSON.stringify(jsonEntity));
}

1 个答案:

答案 0 :(得分:-1)

您能提供更多信息,例如HTTP响应吗?你的OData版本是什么?是V3还是V4?如果是V3,请求应该是

    POST serviceRoot/entityset HTTP/1.1

    DataServiceVersion: 3.0;
    MaxDataServiceVersion: 3.0;
    Content-Type: application/json;odata=minimalmetadata
    Accept: application/json;odata=minimalmetadata
    Accept-Charset: UTF-8

    {
      content of the entity
    }

如果是V4,请求应该是

    POST serviceRoot/EntitySet
    OData-Version: 4.0
    Content-Type: application/json;odata=minimalmetadata
    Accept: application/json
    {
       "@odata.type" : "namespace.type",
       content of the entity
    }

似乎您的代码错过了odata版本标头。