当我将以下内容发布到节点(简化示例)时:
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:3000/action");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify({path:encodeURIComponent("E:\foo\bar.baz")}));
node.js代码:
app.post('/action', function (request, response) {
var file = request.body['path'];
console.log(file);
console.log(decodeURIComponent(file));
});
我得到以下输出:
E%3A%0Coo%08ar.baz
E:♀oar.baz
如何正确解码?
答案 0 :(得分:3)
您在路径中编码special characters,因为反斜杠是为转义而保留的:
\f Form feed
\b Backspace
编码时会变成:
%0C
%08
来自MDN:
要在字符串中包含文字反斜杠,必须转义 反斜杠字符
"E:\\foo\\bar.baz"