使用Web API MS CRM时的JSON错误 [英] JSON error when using Web API MS CRM

查看:80
本文介绍了使用Web API MS CRM时的JSON错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Web API在MS CRM 2016在线实例上关联具有N:N关系的自定义实体的两个记录-自定义实体1"和自定义实体2".我收到以下错误警报-

I am trying to associate two records of custom entities - 'Custom entity 1' and 'Custome Entity 2' having N:N relationship on MS CRM 2016 online instance using Web API. I am getting the following error alert -

从JSON阅读器中读取时,发现了意外的"StartArray"节点.应该是一个"StartObject"节点.

An unexpected ‘StartArray’ node was found when reading from the JSON reader. A ‘StartObject’ node was expected.

以下是代码段-

function associateRequest() {
    var serverURL = Xrm.Page.context.getClientUrl();
    var associate = {
        "@odata.id": serverURL + "/api/data/v8.0/new_customentity1s(08C7365D-4BD1-E511-80EA-3863BB34BA88)"
    };
var req = new XMLHttpRequest();
req.open("POST", serverURL + "/api/data/v8.0/new_customeentity2s(9A1EF77F-4BD1-E511-80EA-3863BB34BA88)/new_new_customentity1_new_customeentity2/$ref", 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) {
            alert('Record Associated');
        } else {
            var error = JSON.parse(this.response).error;
            alert(error.message);
        }
    }
};
req.send(associate);
 }

取消记录的关联正常.

推荐答案

我认为您需要在要发布的对象上使用JSON.stringify:

I think you need to use JSON.stringify on the object you're posting:

var association = {'@odata.id': Xrm.Page.context.getClientUrl() + "/api/data/v8.0/new_tests(37A8B2B7-73C9-E511-80DE-6C3BE5A8DAD0)"};

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/new_test2s(9002CEE2-E9D3-E511-80DD-6C3BE5BD3F5C)/new_new_test_new_test2/$ref", 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) {
        req.onreadystatechange = null;
        if (this.status === 204 || this.status === 1223) {
            //Success - No Return Data - Do Something
        }
        else {
            alert(this.statusText);
        }
    }
};
req.send(JSON.stringify(association));

这篇关于使用Web API MS CRM时的JSON错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆