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

Getting error while Creating Case Record Using Odata Rest Services

本文关键字:错误 记录 案例 Odata Rest 服务 创建 使用      更新时间:2023-09-26

我在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));
}

您能提供更多的信息,如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版本头。