JavaScript编码获取对Microsoft Dynamics CRM的请求,导致语法错误

时间:2019-07-18 14:26:48

标签: javascript crm dynamics-crm-2016

我需要在我们的CRM应用程序中为扇区名称检索相应的ID。

我正在通过JavaScript中的get请求使用CRM的API。我试过使用encodeURI,encodeURIComponent和escape。 get请求适用于某些扇区名称,但其他某些无效,并返回错误。

//The url was masked but the query is the same.
let URL = "http://domain/instance/api/data/v8.2/new_occurrencereportsectors?$select=new_occurrencereportsectorid,new_name&$filter=new_name eq ";

//This part works
let encodedURI = encodeURI(URL);

//This is the string I am trying to pass to the CRM API. This does not work.
let query = "Ontario - Outside the Greenbelt / Ontario - à l'extérieur de la ceinture";

//This is me trying out all the enocdings.
let encodedQuery = encodeURI(query);
encodedQuery = encodeURIComponent(encodedQuery);
encodedQuery = escape(encodedQuery);

//This is the string which I am using for the get request.
let finalString = encodedURI + encodedQuery;

//Note this is an example so I am just putting the printed.
//URL into the search bar in the browser.
console.log(finalString);

我希望返回值是一个ID,格式为{XXXXXXXX}。

输出是语法错误。请参阅以下错误消息。我省去了内部错误,因为它很长,而且我看不到任何错误/

 "error":{
    "code":"","message":"Syntax error: character '%' is not valid at position 19 in 'new_name eq Ontario%2520-%2520Outside%2520the%2520Greenbelt%2520%2F%2520Ontario%2520-%2520%25C3%25A0%2520l'ext%25C3%25A9rieur%2520de%2520la%2520ceinture'."

1 个答案:

答案 0 :(得分:0)

所以我想出了我的问题。 Dynamics CRM不喜欢单引号。函数escape和encodeURI不编码单引号。但是显然在Dynamics CRM 365中,单引号是通过使用两个单引号进行编码的。

例如,“您的名字”将变为“您的名字”。由于参数带有空格,因此在外部保留单引号。