简体   繁体   中英

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?

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, "''") + "')"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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