从JSON读取器读取时,发现意外的'StartObject'节点。预期有一个“ PrimitiveValue”节点

时间:2019-08-17 07:12:28

标签: javascript rest sharepoint-online

我正在使用rest API调用将项目添加到Sharepoint列表中的“电子邮件”人员选择器列中。但由于此错误,我无法添加它。

我曾尝试保持有效负载不带电子邮件,但仍然无法解决

代码是不言自明的,但是如果您愿意的话,我会给您一些启示

setPeoplesColumn: function()
{
    console.log("user id is "+addressBookListRestService.UserID);
    var item=
    {
        "__metadata": 
        {
           "type": 'SP.Data.Address_x0020_BookListItem'
        },   
        "Title": 'Some Dude',
        'EmailId' : 
        {
            "results" : [_spPageContextInfo.userId] 
        }
    };
    $.ajax
    ({  
       url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Address Book')/items",  
       type: "POST",  
       headers: 
       {  
            "accept": addressBookListRestService.acceptHeaderValue,  
            "X-RequestDigest": addressBookListRestService.requestDigestHeaderValue,  
            "content-Type": addressBookListRestService.contentTypeHeaderValue  
        },  
        data:  JSON.stringify(item),
        success: function(data) 
        {  
            console.log("success is "+(data.d.result));  
            console.log("ID is "+JSON.stringify(data.d.results));
        },  
        error: function(error) {  
            console.log("failure is "+JSON.stringify(error));  
        }  
    });//end of ajax function 
},

1 个答案:

答案 0 :(得分:1)

错误应该是json数据格式。

基于您的演示的我的测试脚本(“电子邮件人”字段允许多项选择)。

<script type="text/javascript">
        function setPeoplesColumn() {
            console.log("user id is " + _spPageContextInfo.userId);
            var item =
            {
                "__metadata":
                {
                    "type": 'SP.Data.Address_x0020_BookListItem'
                },
                "Title": 'Some Dude',
                'EmailId':
                {
                    "results": [_spPageContextInfo.userId]
                }
            };
            $.ajax
                ({
                    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Address Book')/items",
                    type: "POST",
                    headers:
                    {
                        "Accept": "application/json;odata=verbose",
                        "Content-Type": "application/json;odata=verbose",
                        "X-RequestDigest": $("#__REQUESTDIGEST").val()
                    },
                    data: JSON.stringify(item),
                    success: function (data) {
                        console.log("success is " + (data.d));
                        console.log("ID is " + JSON.stringify(data.d));
                    },
                    error: function (error) {
                        console.log("failure is " + JSON.stringify(error));
                    }
                });//end of ajax function
        }
    </script>

    <input id="Button1" type="button" onclick="setPeoplesColumn()" value="button" />