简体   繁体   English

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

[英]JavaScript Encoding Get Request to Microsoft Dynamics CRM causing syntax error

I need to retrieve the corresponding ID for a sectors name in our CRM application. 我需要在我们的CRM应用程序中为扇区名称检索相应的ID。

I am using the CRM's API through a get request in JavaScript. 我正在通过JavaScript中的get请求使用CRM的API。 I have tried using encodeURI, encodeURIComponent, and escape. 我试过使用encodeURI,encodeURIComponent和escape。 The get request works with some sector names but others do not work and return an error. 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);

I expected the return value to be an ID which will be in the format {XXXXXXXX}. 我希望返回值是一个ID,格式为{XXXXXXXX}。

The output is a syntax error. 输出是语法错误。 Please see below for the error message. 请参阅以下错误消息。 I left out the inner error because it is lengthy and I didn't see it telling anything/ 我忽略了内部错误,因为它很长,而且我看不到它在说什么/

 "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'."

So I figured out my issue. 所以我想出了我的问题。 Dynamics CRM wasn't liking single quotes. Dynamics CRM不喜欢单引号。 The functions escape and encodeURI don't encode single quotes. 函数escape和encodeURI不编码单引号。 But apparently in Dynamics CRM 365 single quotes are encoded by using two single quotes. 但是显然在Dynamics CRM 365中,单引号是通过使用两个单引号进行编码的。

For example 'You're name', will become 'You''re name'. 例如,“您的名字”将变为“您的名字”。 Leaving a single quote on the outside since the parameter has spaces. 由于参数带有空格,因此在外部保留单引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM