Search code examples
javascriptjquerysyntaxodatacall

JavaScript: Issue with single quotes and URL


I have the following JS coding:

var myVariable = "material'";   //<- there is a single quote before the double quote!

var object = {
path: "/Data(Material='" + myVariable + "')"
}

object.path is building a URL for a service call and will result in this:

https://myurl/Data(Material='material'')

Of course this service call will fail because of the two single quotes.

What else can I do?


Solution

  • I think this should take care of it. Just replace all single quotes in your variable for two single quotes:

    var object = {
        path: "/Data(Material='" + myVariable.replace(/'/g, "''") + "')"
    }