NodeJS转义反斜杠 [英] NodeJS escaping back slash

查看:1226
本文介绍了NodeJS转义反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在转义反斜杠时遇到了一些问题,下面是我尝试过的代码段。问题是如何将带有转义斜杠的变量分配给另一个变量。

I am facing some issues with escaping of back slash, below is the code snippet I have tried. Issues is how to assign a variable with escaped slash to another variable.

var s = 'domain\\username';
var options = {
    user : ''
};
options.user =  s;

console.log(s); // Output : domain\username - CORRECT
console.log(options); // Output : { user: 'domain\\username' } - WRONG

为什么我正在打印选项对象两个斜杠都来了吗?

Why when I am printing options object both slashes are coming?

我感觉自己在这里做的事情确实/非常错误,这可能是基础。

I had feeling that I am doing something really/badly wrong here, which may be basics.

当我使用此对象 options 时,值传递为它是(带有双斜杠),并且我将其与SOAP服务一起使用,并且由于无效的用户属性值而收到401错误。

When I am using this object options the value is passing as it is (with double slashes), and I am using this with my SOAP services, and getting 401 error due to invalid user property value.

但是当我使用相同的用户值尝试对PHP代码进行相同操作时,它会给出正确的响应,在PHP中,我们也用两个斜杠将值转义。

But when I tried the same with PHP code using same user value its giving proper response, in PHP also we are escaping the value with two slashes.

推荐答案

输出正确。

设置变量时,转义的反斜杠为

When you set the variable, the escaped backslash is interpreted into a single codepoint.

但是,选项是一个对象,在登录时会显示为JSON blob 。此时,反斜杠将重新转义,因为这是反斜杠可以有效地显示为JSON输出中的字符串值的唯一方法。

However, options is an object which, when logged, appears as a JSON blob. The backslash is re-escaped at this point, as this is the only way the backslash can appear validly as a string value within the JSON output.

console.log(options)到javascript中的JSON输出(使用 JSON.parse()或类似的语言),然后输出用户密钥,只会显示一个反斜杠。

If you re-read the JSON output from console.log(options) into javascript (using JSON.parse() or similar) and then output the user key, only one backslash will show.

(以下问题编辑:)

为了使您的数据能够被SOAP消费服务接受,数据需要在带内显式转义。在这种情况下,您需要在分配值时对其进行两次转义:

It is possible that for your data to be accepted by the SOAP consuming service, the data needs to be explicitly escaped in-band. In this case, you will need to double-escape it when assigning the value:

var s = 'domain\\\\user'

要明确确定您是否需要这样做,建议您在工作的PHP应用程序和SOAP应用程序之间放置一个代理,并检查流量。

To definitively determine whether you need to do this or not, I'd suggest you put a proxy between your working PHP app and the SOAP app, and inspect the traffic.

这篇关于NodeJS转义反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆