简体   繁体   English

JavaScript:单引号和URL出现问题

[英]JavaScript: Issue with single quotes and URL

I have the following JS coding: 我有以下JS编码:

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: object.path正在为服务调用构建URL,这将导致以下结果:

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

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

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