最近用WebApi作基于Fetchxml的查询的时候,遇到一个很蛋疼的报错:Invalid URI: The Uri scheme is too long.html
检查了整个URL,也没发现有什么问题.json
----------------------------------------------------api
我这边是基于原来的代码作了一点点改动,就是把基于Date的查询修改成基于DateTime.app
原来查询条件是:post
<condition attribute="createdon" operator="on-or-before" value='2019-06-01' />fetch
<condition attribute="createdon" operator="on-or-after" value='2019-06-12' />ui
修改后的条件==>编码
<condition attribute="createdon" operator="on-or-before" value='2019-06-01 16:00:00'' />spa
<condition attribute="createdon" operator="on-or-after" value='2019-06-12' 16:00:00 />code
而后就出现了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的区别,可看下这篇博文,有比较形象的说明:http://www.javashuo.com/article/p-sedffpmp-ko.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' />
到这里,问题才是真正的获得了解决~~