【问题标题】:why 'PrimitiveValue' or 'StartObject' node was expected error为什么“PrimitiveValue”或“StartObject”节点是预期错误
【发布时间】:2020-03-21 07:27:25
【问题描述】:

我正在使用 SharePoint Online:

我有一个包含几个多选字段的表单。当我尝试添加项目时,我收到以下错误:

"value: "在尝试读取属性值时,从 JSON 读取器中读取了“StartArray”类型的节点;但是,需要一个 'PrimitiveValue' 或 'StartObject' 节点。”

这是多选的代码和字段。

 $.ajax({
url: fullUrl,
method: "POST",
data: JSON.stringify({
  '__metadata': { 'type': 'SP.Data.AuditItem' },
  'Register': that.register,
  'RiskRegister': that.nextIndex,
  'Reopen': that.formatDate(that.reopen),
  'RiskOrIssue': that.riskOrIssue,
  'Locations': that.locations,               //<---multi-select choice field
  'ProblemT': that.probTitle,
  'ProblemStatement': that.problemStatement,
  'TaskOwner': that.taskOwner,
  'RiskOwner': that.riskOwner,
  'Auditor': that.auditor    //<--- multi-select choice field
}),
headers: {
  "accept": "application/json;odata=verbose",
  "content-type": "application/json;odata=verbose",
  "X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(){
  alert("Item Added!");
},
error: function(data){
  console.log(data);
}   
});    

我该如何解决这个问题?

谢谢!

【问题讨论】:

    标签: jquery rest sharepoint sharepoint-online


    【解决方案1】:

    多选选择字段值需要用集合来设置,这里有一个代码sn-p供大家参考:

       <script type="text/javascript">
           var locations = ['Locations1','Locations3'];
           var Auditors =  ['Auditor1','Auditor3'];
           var item = {  
                        "__metadata": {  
                            "type": 'SP.Data.MyListListItem'
                        },  
                           "Title":'Test',
                           "Locations": { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: locations },
                           "Auditor": { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: Auditors }
    
    
                    };    
      $.ajax({  
                        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('MyList')/items",  
                        method: "POST",  
                        contentType: "application/json;odata=verbose",  
                        data: JSON.stringify(item),  
                        async: false,  
                        headers: {  
                            "Accept": "application/json;odata=verbose",  
                            "X-RequestDigest": $("#__REQUESTDIGEST").val() 
                        },  
                        success: function(data) {  
                            alert('The Request has been successfully Added'); 
                        },  
                        error: function(jqXHR, textStatus, errorThrown) {  
                            console.log(jqXHR.responseText);
                            alert('Error');  
                        }  
                    });  
    </script>
    

    参考:

    Update multiple choice field in sharepoint using rest api

    【讨论】:

    • 您正在对数组值进行硬编码并且它正在工作。当我这样做时 ``` "Locations": { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: that.locations},``` 它不起作用。 that.locations 包含一个数组。
    • 请检查 that.locations 数组是否有效,我用数组变量测试过,仍然有效,我将用数组变量更新回复。
    • 我真的不明白为什么它不起作用。 Array.isArray(that.locations) 返回真
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2021-12-19
    • 1970-01-01
    • 2019-11-01
    • 2014-07-21
    • 1970-01-01
    • 2012-04-14
    相关资源
    最近更新 更多