body-parser : 如何处理节点请求中收到的'&'?



当我将数据发送到我的nodeJS应用程序时,我的"&"字符有问题。

如果我发送此请求:

var str = JSON.stringify({ myValue: 'hello&world' });
fetch(myAPIpath, {
method: 'post',
body: `values=${str}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});

我在我的 Node 应用程序中解析请求:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

我有以下错误:

语法错误:JSON 输入意外结束

事实上,当我记录 req.body.values 时,我有这个:

{"values":"hello

你知道怎么解决这个问题吗?这是否意味着我无法在正文请求中发送"&"字符?

通常 & 是一个特殊字符,HTML 编码为 &。为了解释这种转义,特殊字符通过 .例如,这里发送 \&。当您想在请求内用引号发送内容时,您也可以看到这一点。

最新更新