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' />

 

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

 

As an AI language model, I don't have access to the content of the file './docker/docker-compose-test-net.yaml'. However, here are some common reasons that a Docker Compose file may be invalid: 1. Syntax errors: The file may contain syntax errors such as missing brackets, commas, or quotation marks. 2. Incorrect indentation: The indentation of the file may be incorrect, causing errors in the parsing of the file. 3. Incorrect version number: The version number of the file may be incorrect, causing the file to be invalid. 4. Invalid or misspelled service names: The names of the services defined in the file may be misspelled or invalid. 5. Invalid or missing network configuration: The network configuration in the file may be invalid or missing, causing errors in the creation of the network. 6. Invalid or missing environment variables: Environment variables defined in the file may be invalid or missing, causing errors in the execution of the services. 7. Invalid volumes or mounts: The volumes or mounts defined in the file may be invalid, causing errors in the mounting of the directories or files. 8. Invalid or missing dependencies: The dependencies defined in the file may be invalid or missing, causing errors in the execution of the services. 9. Incorrect or missing container configurations: The configurations of the containers defined in the file may be incorrect or missing, causing errors in the creation or execution of the containers. 10. Invalid or missing health checks: The health checks defined in the file may be invalid or missing, causing errors in the monitoring of the containers.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值