在JScript中可以同步,也可以异步地使用REST Endpoint。以Retrieve为例,在下面的代码中,retrieveRecordsReq.open("GET", oDataPath + filter, false) 的第二个参数是false即为同步调用,如果为true即为异步调用。代码中其他部分同步与异步完全相同。
if (typeof (ContactReadPaneLibrary) == "undefined") { ContactReadPaneLibrary = { __namespace: true }; } ContactReadPaneLibrary = { Name: "ContactReadPaneLibrary", OnLoad: function () { if (parent.window.Xrm.Page.getAttribute("primarycontactid").getValue() != null && parent.window.Xrm.Page.getAttribute("primarycontactid").getValue().length > 0) { var primartycontact = parent.window.Xrm.Page.getAttribute("primarycontactid").getValue()[0]; var contactid = primartycontact.id; var oDataPath = Xrm.Page.context.prependOrgName("/xrmservices/2011/organizationdata.svc"); var filter = "/ContactSet(guid'" + contactid + "')?$select=Telephone1,FullName"; var retrieveRecordsReq = new XMLHttpRequest(); retrieveRecordsReq.open("GET", oDataPath + filter, false); retrieveRecordsReq.setRequestHeader("Accept", "application/json"); retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); retrieveRecordsReq.onreadystatechange = function () { ContactReadPaneLibrary.RetrieveContactCallBack(this); }; retrieveRecordsReq.send(); } }, RetrieveContactCallBack: function (retrieveRecordsReq) { if (retrieveRecordsReq.readyState == 4 /* complete */) { if (retrieveRecordsReq.status == 200) { //Success //alert(retrieveRecordsReq.responseText); if (retrieveRecordsReq.responseText) { var retrievedRecord = JSON.parse(retrieveRecordsReq.responseText).d; document.getElementById("TextFullname").value = retrievedRecord.FullName; document.getElementById("TextBusinessPhone").value = retrievedRecord.Telephone1; } } else { alert("Error : " + retrieveRecordsReq.status + ": " + retrieveRecordsReq.statusText + ": " + JSON.parse(retrieveRecordsReq.responseText).error.message.value); } } } }
本文转自JF Zhu博客园博客,原文链接: http://www.cnblogs.com/jfzhu/archive/2013/02/12/2910622.html ,如需转载请自行联系原作者