Dynamics CRM Web Api功能:使用encodeURIComponent时路径中的非法字符 [英] Dynamics CRM Web Api Function: Illegal characters in path when using encodeURIComponent

查看:117
本文介绍了Dynamics CRM Web Api功能:使用encodeURIComponent时路径中的非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用搜索功能( https://msdn. microsoft.com/en-us/library/mt608029.aspx )通过Dynamics CRM 2016 Web API.这是我的代码:

I'm trying to use the Search Function (https://msdn.microsoft.com/en-us/library/mt608029.aspx) via the Dynamics CRM 2016 Web API. This is my code:

var start = new Date(2016, 2, 1, 17, 0, 0);
var end = new Date(2016, 2, 10, 18, 0, 0);

var request = new Object();
request.AppointmentRequest = new Object();
request.AppointmentRequest.SearchWindowStart = start.toISOString();
request.AppointmentRequest.SearchWindowEnd = end.toISOString();
request.AppointmentRequest.ServiceId = "5f3b6e7f-48c0-e511-80d7-d89d67631c44";
request.AppointmentRequest.Direction = 0;
request.AppointmentRequest.NumberOfResults = 10;
request.AppointmentRequest.UserTimeZone = 1;

var req = new XMLHttpRequest()
req.open("GET", clientUrl + "/api/data/v8.0/Search(" + encodeURIComponent( JSON.stringify(request) ) +")", true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
    if (req.readyState == 4 && req.status == 200) {
        alert(req.responseText);
    }
     else {
        alert(req.response);
    }
};
req.send();

最初使用CRM Online尝试此操作时,出现以下错误:

When I initially tried this using CRM Online I received the following error:

发生了错误.

"An error has occurred.

重试此操作.如果问题仍然存在,请检查Microsoft Dynamics> CRM社区以获取解决方案,或与您组织的Microsoft> Dynamics CRM管理员联系.最后,您可以联系Microsoft支持."

Try this action again. If the problem continues, check the Microsoft Dynamics >CRM Community for solutions or contact your organization's Microsoft >Dynamics CRM Administrator. Finally, you can contact Microsoft Support."

当我在web.config中使用DevErrors ="On"在本地部署中尝试此操作时,我在事件查看器中看到以下错误:

When I try this with an On-Premise deployment with DevErrors="On" in the web.config, I see the following error in the Event Viewer:

异常信息: 异常类型:HttpException 异常消息:>从客户端(:)检测到潜在危险的Request.Path值. 在System.Web.HttpRequest.ValidateInputIfRequiredByConfig() 在System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext> context)

Exception information: Exception type: HttpException Exception message: A potentially dangerous Request.Path value was detected >from the client (:). at System.Web.HttpRequest.ValidateInputIfRequiredByConfig() at System.Web.HttpApplication.PipelineStepManager.ValidateHelper(HttpContext >context)

请求信息: 请求网址: http://win-0e5dfqgqorm:444/ORG/api/data/v8.0/Search ({{"AppointmentRequest":{"SearchWindowStart":"2016-03-01T17:00:00.000Z","SearchWindowEnd":"2016-03-10T18:00:00.000Z ," ServiceId:" 5f3b6e7f-48c0-e511-80d7-d89d67631c44,"方向:0," NumberOfResults:10," UserTimeZone:1}}) 请求路径:/SHUDEV/api/data/v8.0/Search({"AppointmentRequest":{"SearchWindowStart":"2016-03-01T17:00:00.000Z","SearchWindowEnd":"2016-03-10T18: 00:00.000Z," ServiceId:" 5f3b6e7f-48c0-e511-80d7-d89d67631c44,"方向:0," NumberOfResults:10," UserTimeZone:1}})

Request information: Request URL: http://win-0e5dfqgqorm:444/ORG/api/data/v8.0/Search({"AppointmentRequest":{"SearchWindowStart":"2016-03-01T17:00:00.000Z","SearchWindowEnd":"2016-03-10T18:00:00.000Z","ServiceId":"5f3b6e7f-48c0-e511-80d7-d89d67631c44","Direction":0,"NumberOfResults":10,"UserTimeZone":1}}) Request path: /SHUDEV/api/data/v8.0/Search({"AppointmentRequest":{"SearchWindowStart":"2016-03-01T17:00:00.000Z","SearchWindowEnd":"2016-03-10T18:00:00.000Z","ServiceId":"5f3b6e7f-48c0-e511-80d7-d89d67631c44","Direction":0,"NumberOfResults":10,"UserTimeZone":1}})

JSON对象已编码,因此我不确定为什么检测到非法字符. Web Api的SDK文档比较简短,并且没有过多地介绍如何将ComplexType传递给Web Api函数,是否有人曾经/已经设法将ComplexType传递给Web Api函数?

The JSON object is encoded so I'm not sure why it's detected illegal characters. The SDK documentation for the Web Api is light and doesn't go into too much detail as to how to pass a ComplexType to a Web Api function, has anyone seen this issue before/managed to pass a ComplexType to a Web Api function?

谢谢.

推荐答案

我设法解决了此问题.关键是将JSON对象作为查询参数传递进来:

I managed to resolve this issue. The key is to pass the JSON object in as a query parameter:

var request = new Object();
request.SearchWindowStart = start.toISOString();
request.SearchWindowEnd = end.toISOString();
request.ServiceId = "5f3b6e7f-48c0-e511-80d7-d89d67631c44";
request.Direction = '0';
request.NumberOfResults = 10;
request.UserTimeZoneCode = 1;

var req = new XMLHttpRequest()
req.open("GET", clientUrl + "/api/data/v8.0/Search(AppointmentRequest=@request)?@request=" + JSON.stringify(request) , true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) {
    alert(req.responseText);
}
else {
    alert(req.response);
    }
};
req.send();

SDK中对此进行了记录: https://msdn.microsoft.com /en-us/library/gg309638.aspx .

This is documented in the SDK: https://msdn.microsoft.com/en-us/library/gg309638.aspx.

希望这可以帮助遇到类似问题的任何人.

Hope this helps anyone who runs into a similar issue.

这篇关于Dynamics CRM Web Api功能:使用encodeURIComponent时路径中的非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆