如何转义 OData 查询中使用的单引号?
我正在使用 OData 查询我的数据库。当“adapterName”仅包含文本时,以下代码行可以正常工作。
ds.query('/DataAdapters?$filter=Name eq \'' + adapterName + '\'', ifmgr_CreateAdapter_Step1, onGenericFailure, '');
如果“adapterName”包含单引号,则会失败。我尝试使用以下代码转义单引号:
adapterName = adapterName.replace(/\'/g, '\\\'');
尽管这正确地转义了用户定义的文本,但该函数仍然失败。谁能告诉我查询中文本的正确格式是什么?
I am using OData to query my database. The following line of code works fine when “adapterName” just contains text.
ds.query('/DataAdapters?$filter=Name eq \'' + adapterName + '\'', ifmgr_CreateAdapter_Step1, onGenericFailure, '');
If “adapterName” contains a single quote it fails. I tried escaping the single quote by using the following code:
adapterName = adapterName.replace(/\'/g, '\\\'');
Although this correctly escapes the user defined text the function still fails. Can anyone tell me what the correct format is for text in the query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上 %27 不是一个解决方案。正确的转义方法是在字符串中放置两个单引号,而不是一个。
在示例中
“o''clock”
Actually %27 is not a solution. The correct way to escape is to place two single quotes into the string instead one.
In example
"o''clock"
我想稍微扩展一下答案,以便它也适用于调用 oData 服务操作操作。发布的答案是正确的,但是服务操作的参数必须按照特定的顺序进行编码。
oData 服务操作接收原始类型参数,其中字符串包含在 ' 中,这样有效的 url(预编码)将是这样的
AddString?value='o''clock'
这将导致服务器看到
AddString?value='o '
和
'clock'
将产生“错误请求 - 查询语法错误”。
要纠正此问题,您必须在插入 url 之前对 ' 进行双重转义并对其进行 UrlEncode。
不要对 url 本身进行 UrlEncode。
这是一个可行的示例。
I want to expand upon the answer a bit so that it also applies to calling an oData Service Operation Action. The answer posted answer is correct, but there is a specific order in which the parameters to a service operation must encoded.
oData Service Operations receive primitive type parameters where strings are enclosed in a ' such that a valid url (pre encoding) will be as such
AddString?value='o''clock'
This will cause the server to see
AddString?value='o'
and
'clock'
will produce "Bad Request - Error in query syntax."
To correct this, you must double escape the ' and UrlEncode it prior to insertion into the url.
Do not UrlEncode the url itself.
Here's an example that will work.
它实际上在 oData 文档中进行了描述: http://docs.oasis-open.org/odata/odata/v4.01/cs01/part2-url-conventions/odata-v4.01-cs01- part2-url-conventions.html#sec_URLComponents
It's actually described in oData docs: http://docs.oasis-open.org/odata/odata/v4.01/cs01/part2-url-conventions/odata-v4.01-cs01-part2-url-conventions.html#sec_URLComponents
当使用 wit substringof 时,需要使用 4 个而不是 1 个撇号来转义:
a'b->
$filter=(substringof('a'''b', FirstName))
When using wit substringof it needs to be escaped by having 4 instead of 1 apostrophe:
a'b ->
$filter=(substringof('a''''b', FirstName))
我没有使用 $filter=Title eq 'text',而是
使用 oDatastartswith() 函数。
$filter=startswith(Title, key)
然后我传入尽可能多的 key 。
var pos = key.indexOf("'");
if(pos > -1) {
key = key.substring(0, pos);
<代码>}
Instead of using $filter=Title eq 'text'
I am using the oData startswith() function.
$filter=startswith(Title, key)
and then I pass in as much of key as I can.
var pos = key.indexOf("'");
if(pos > -1) {
key = key.substring(0, pos);
}