我正在使用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);
}
});
我该如何解决这个问题?
谢谢!
答案 0 :(得分:1)
多选选择字段值需要设置一个集合,这是一个代码段供您参考:
<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>
参考: