Dynamics CRM - js中用webapi基于fetchxml查询遇到的问题 -- Invalid URI: The Uri scheme is too long.

最近用WebApi做基于Fetchxml的查询的时候,遇到一个很蛋疼的报错:Invalid URI: The Uri scheme is too long.

检查了整个URL,也没发现有什么问题.

----------------------------------------------------

我这边是基于原来的代码做了一点点改动,就是把基于Date的查询修改为基于DateTime.

原来查询条件是:

<condition attribute="createdon" operator="on-or-before" value='2019-06-01' />

<condition attribute="createdon" operator="on-or-after" value='2019-06-12' />

修改后的条件==>

<condition attribute="createdon" operator="on-or-before" value='2019-06-01  16:00:00'' />

<condition attribute="createdon" operator="on-or-after" value='2019-06-12'  16:00:00 />

然后就出现了Invalid URI: The Uri scheme is too long.的错误信息.

于是乎,开始了艰辛的问题排查.

初步发现的问题:

1.on-or-before\on-or-after并不支持时分秒

2.如果把查询的字段减少一部分,则不会报错

-----修改代码继续排错-----修改后的条件==>--------

<condition attribute="createdon" operator="ge" value='2019-06-01 16:00:00' />

<condition attribute="createdon" operator="lt" value='2019-06-12 16:00:00' />

然而,还是同样的问题(减少一部分查询字段,跟上面一样,不会报错)...

既然还是提示too long..那我就把GET改为POST,总该行了吧..

 

var fetchUri = "http://xxxxxx/api/data/v8.2/$batch";

var body = [];
body.push('--batch_postfetch');
body.push('Content-Type: application/http');
body.push('Accept: application/json');
body.push('Content-Transfer-Encoding: binary');
body.push('');
body.push('GET ' +  "http://xxxxxx/api/data/v8.2/accounts"  + "?fetchXml=" + encodeURI(fetchXMLQuery) + ' HTTP/1.1');
body.push('Content-Type: application/json');
body.push('Accept: application/json');
body.push('OData-Version: 4.0');
body.push('OData-MaxVersion: 4.0');
body.push('Prefer:odata.include-annotations=OData.Community.Display.V1.FormattedValue');
body.push('');
body.push('--batch_postfetch--');

var req = new XMLHttpRequest();     
req.open("POST", fetchUri, true);
req.setRequestHeader("Content-Type", 'multipart/mixed;boundary=batch_postfetch');
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
req.onreadystatechange = function () {//此处省略1w行代码}

req.send(body.join('\r\n'));

没错,还是同样的问题..在一次偶然的机会中,发现在CRM REST Builder的Predefined Query中查询,则可以正常查询~

fetchXML还是那段fetchXML,唯一的区别则是我的代码,xml是经过了encodeURI进行编码.

于是乎,用encodeURIComponent编码后再进行查询,正确运行...

想了一下,大概原因是,时分秒那里包含了冒号:,而encodeURI遇到冒号:时,并不会对其进行转义;

关于encodeURI跟encodeURIComponent的区别,可看下这篇博文,有比较形象的说明:https://www.cnblogs.com/shuiyi/p/5277233.html

-------------------------------

至此,问题似乎已经得到了解决,其实,还有一个细节上的东西,就是在用le,ge,lt,gt操作符的时候,如果记得使用UTC格式的时间

所以,最终我们的condition还需要做下修改:

<condition attribute="createdon" operator="ge" value='2019-06-01T16:00:00Z' />

<condition attribute="createdon" operator="lt" value='2019-06-12T16:00:00Z' />

 

到这里,问题才是真正的得到了解决~~

 

posted @ 2019-06-13 09:49  Chobohoo  阅读(1017)  评论(0编辑  收藏  举报