我使用的是IFD CRM2011环境,我正在跟踪此MSDN示例查询ODATA端点,以在IFrame中填充下拉菜单。
下面是填充下拉列表的代码:
function GetQuestionSetList() {
var query = '/Mhc_questionsetverSet?' +
'$select=Mhc_name,Mhc_questionsetverId&$filter=statecode/Value eq 0';
SDK.RestEndpointPaging
.RetrieveRecords(query, ProcessReturnedQuestionSetVersions);
}
function ProcessReturnedQuestionSetVersions(retrievedQuestionSets) {
for (var i = 0; i < retrievedQuestionSets.length; i++) {
var questionSet = retrievedQuestionSets[i];
var value = questionSet.Mhc_questionsetverId;
var name = questionSet.Mhc_name;
//add option to select list
$('#selectQuestionSetVersion').append($('<option>')
.attr('value', value)
.text(name));
}
}在执行SDK.RestEndpointPaging.RetrieveRecords(query, ProcessReturnedQuestionSetVersions);行之后,将提示我使用以下对话框:

此时,我可以输入凭据或按下cancel,然后填充下拉列表。在开发人员工具中,我注意到这两种情况下的错误:
SCRIPT5022:引发和未捕获的异常 mhc_json2.js,第484行字符13
// If the text is not JSON parseable, then a SyntaxError is thrown.
throw new SyntaxError('JSON.parse'); //line 484
};
}
}());我不知道为什么会出现这个对话框,或者为什么会抛出这个错误。
发布于 2012-01-05 22:01:54
发了这篇文章后,我碰巧检查了一下小提琴,发现了这个问题。在我的情况下,有两个调用RetrieveRecords。第一次呼叫返回状态200 (成功),而第二次调用返回401。
以下是两个电话:
问答&$filter=statecode/Value eq 0 '%7B3F737386-54DF-DE11-A55C-00155D020C0D%7D','%7B7AF1B564-C3BF-DD11-8209-000BCDC54FC9%7D'
在第二个调用中,服务器+ odata端点连接两次。如果发现了如下所示的RetrieveRecordsCallback参数,则__next函数将剥离服务器和端点url:
MSDN解释: 如果找到__next属性,则将带有$skiptoken的新URL作为新的筛选器参数值传递回SDK.RestEndpointPaging.RetrieveRecords,以便对每组记录重复处理,直到检索到所请求记录的总数>数量,并且不再返回__next属性。
if (null != retrievedRecords.__next) {
// The existance of the '__next' property
//indicates that more records are available
// So the originating function is called again
//using the filter value returned
var filter = retrievedRecords.__next
.replace(SDK.RestEndpointPaging.GetODataPath(), "");
SDK.RestEndpointPaging.RetrieveRecords(filter, callback);
}SDK.RestEndpointPaging.GetODataPath()在url的服务器和端点部分之间附加了一个额外的/,但是当返回新的筛选器参数时,额外的/已被删除,因此.replace函数无法替换路径,并被追加第二次。
修复是微不足道的。只需将SDK.RestEndpointPaging.GetODataPath()函数更改为:
GetODataPath: function () {
/// <summary>
/// Utility function to retrieve the path to the REST endpoint.
/// </summary>
var serverUrl = Xrm.Page.context.getServerUrl();
//remove the extra '/' char if it exists
if (serverUrl.match(/\/$/)) {
serverUrl = serverUrl.substring(0, serverUrl.length - 1);
}
return serverUrl + "/xrmservices/2011/organizationdata.svc";
},https://stackoverflow.com/questions/8750266
复制相似问题