如何将 ODATA 结果中的信息检索到 JavaScript 变量中?

我正在使用 SharePoint 创建一个按钮,您可以单击该按钮从在该 SharePoint 上创建的列表中检索列表条目,并根据检索到的内容填充一系列文本区域。然后,这将用于编辑和发回,或填充到 html 格式的模板中以通过电子邮件发送。


到目前为止,我有 ODATA 电话:


function retrieveData(){

var incidentID = document.getElementById("IncidentRef");


var requestUri = "https://site/_api/Web/Lists/GetByTitle('Incident List')/Items?$Filter=Title eq '" + incidentID + "'&$select=Title,Id,ImpactedArea,IncidentStatus,Impact,AdvisorActions,Update";

}

从这里,我可以得到一个 XML 响应,它显示了我想要从列表中成功检索的所有内容 - 我不知道该怎么做是将它从 ODATA XML 响应传输到我可以的 JavaScript 变量中然后分配给.value我为显示此信息而设置的各种文本区域。我读到您可以将数据作为 JSON 文件检索 - 这会将数据存储在控制台中以便我可以从那里提取数据吗?如果是这样,您如何从中检索数据?


烙印99
浏览 197回答 2
2回答

慕桂英4014372

以下示例代码供您参考。<input id="IncidentRef" type="text"/><input type="button" value="GetItem" onclick="getListItem()"/><p>Title: <input id="Title" type="text"/></p><p>ImpactedArea: <input id="ImpactedArea" type="text"/></p><p>IncidentStatus: <input id="IncidentStatus" type="text"/></p><p>Impact: <input id="Impact" type="text"/></p><p>AdvisorActions: <input id="AdvisorActions" type="text"/></p><p>Update: <input id="Update" type="text"/></p><script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script><script type="text/javascript">function getListItem(){&nbsp; &nbsp; var listName="Incident List";&nbsp; &nbsp; var incidentID = $("#IncidentRef").val();&nbsp; &nbsp; if(incidentID!=""){&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('"+listName+"')/items?$filter=Title eq '" + incidentID + "'&$select=Title,Id,ImpactedArea,IncidentStatus,Impact,AdvisorActions,Update",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "GET",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Accept": "application/json;odata=verbose",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(data.d.results.length>0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var item=data.d.results[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#Title").val(item.Title);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#ImpactedArea").val(item.ImpactedArea);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#IncidentStatus").val(item.IncidentStatus);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#Impact").val(item.Impact);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#AdvisorActions").val(item.AdvisorActions);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#Update").val(item.Update);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //alert("Error");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}</script>

慕无忌1623718

要获取 JSON,请将以下内容添加为 REST 请求中的标头。(您是使用 jQuery AJAX 还是...?){"accept": "application/json; odata=verbose"}或以下任何一项:{"accept": "application/json; odata=minimalmetadata"}{"accept": "application/json; odata=nometadata"}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript