Dynamics CRM - js中用webapi基於fetchxml查詢遇到的問題 -- Invalid URI: The Uri scheme is too long.

最近用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' />

 

到這裏,問題纔是真正的獲得瞭解決~~

相關文章
相關標籤/搜索