Javascript:用双反斜杠替换反斜杠(Javascript: Replace backslashes with double backslashes)

编程入门 行业动态 更新时间:2024-10-11 01:17:09
Javascript:用双反斜杠替换反斜杠(Javascript: Replace backslashes with double backslashes)

我目前正在解决标题中提到的问题,而且我在某种程度上找不到用双反斜杠正确替换反向的方法,这样我就可以正确地将字符串作为参数提供给web服务。 让我告诉你我的尝试。 其中一些确实适用于其他人,但不适合我...我正在使用FF18.0.1进行测试

WSParameters.replace(/\\/g, "\\\\\\\\"); WSParameters.replace("\\", "\\\\\\\\"); WSParameters.replace(/\\/g, "\\\\"); WSParameters.replace(/\\/g, "\\"); WSParameters.replace(/\\/g, "\"); WSParameters.replace("\\", "\\\\");

非常感谢提前

编辑:我应该提到它以某种方式解析为JSON和firebug我看到源字符串中的反斜杠,但不是在JSON视图中。 也许有另一种方式? 但不知何故,它已经失败了更换反斜杠。

EDIT2:

if (noAction == false) { $.ajax({ type: "POST", url: "WebService.asmx/" + webMethod, contentType: "application/json; charset=utf-8", dataType: "json", async: pAsync, data: WSParameters, success: function callFunction(result) { processPOSTResults(result, pType, pNot);}, error: function (xhr, ajaxOptions, thrownError) { alert('Error while communicating with WebAdmin web service. - ' + xhr.status + " " + thrownError); } }); }

I'm currently having the problem mentioned in the title and I'm somehow not finding a way to properly replace backsashes with double backslashes, so that I can properly give the string to a webservice as parameters. Let me show you what I tried. Some of these do actually work for some other people, but not for me... I'm currently testing this with FF18.0.1

WSParameters.replace(/\\/g, "\\\\\\\\"); WSParameters.replace("\\", "\\\\\\\\"); WSParameters.replace(/\\/g, "\\\\"); WSParameters.replace(/\\/g, "\\"); WSParameters.replace(/\\/g, "\"); WSParameters.replace("\\", "\\\\");

Thanks a lot in advance

EDIT: I should mention that it's somehow parsed into JSON and with firebug I see the backslash in the source string, but not in the JSON view. Maybe there is another way? But somehow it's already failing at the replacement of the backslashes.

EDIT2:

if (noAction == false) { $.ajax({ type: "POST", url: "WebService.asmx/" + webMethod, contentType: "application/json; charset=utf-8", dataType: "json", async: pAsync, data: WSParameters, success: function callFunction(result) { processPOSTResults(result, pType, pNot);}, error: function (xhr, ajaxOptions, thrownError) { alert('Error while communicating with WebAdmin web service. - ' + xhr.status + " " + thrownError); } }); }

最满意答案

WSParameters.replace(/\\/g, "\\\\"); 应该这样做,也在FF18中。 请注意,如果您使用JSON.stringify ,则会自动完成。 还要注意许多控制台输出(Firebug等)用引号包围字符串内容,但不要逃避它们。

WSParameters.replace(/\\/g, "\\\\"); should do it, and in FF18 as well. Notice that if you use JSON.stringify, this is done automatically. Also watch out that many console outputs (Firebug etc) do surround string contents with quotes, but do not escape them.

更多推荐

本文发布于:2023-08-04 18:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1419246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   用双反   Javascript   double   backslashes

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!