7
votes

I tried to escape the single quote when preparing the query in JS this way:

_value.replace(/'/g,'%27')

and this way:

_value.replace(/\'/g,'\\\'');

both doesn't seem to work

You can see an example here: http://services.odata.org/V3/Northwind/Northwind.svc/Orders?$select=Freight,CustomerID&$filter=ShipName+eq+'B's%20Beverages'&$format=json

Does anyone know how to escape the single quote?

Thanks

3

3 Answers

14
votes

The single quote need to be doubled, for instance:

ShipName+eq+'B''sBeverages'

instead of

ShipName+eq+'B'sBeverages'
0
votes

Used this code to replace single quote... Its working..

_value.replace(/'/g, '%27%27')
0
votes

as per example 3 of http://docs.oasis-open.org/odata/odata/v4.01/cs01/part2-url-conventions/odata-v4.01-cs01-part2-url-conventions.html#sec_URLComponents

you need escape a quote with a quote (also, don't forget to handle & : encode to %26 )

so "bit's & bobs" becomes "bit''s %26 bobs"