Intereting Posts

decodeURIComponent manglespath

当我将以下内容发布到节点(简化示例)时:

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 

我如何正确解码?

Solutions Collecting From Web of "decodeURIComponent manglespath"

您正在编码path中的特殊字符 ,因为反斜杠是为转义保留的:

\f Form feed

\b Backspace

当编码这些成为:

%0C

%08

来自MDN :

要在string中包含文字反斜杠,您必须转义反斜杠字符

"E:\\foo\\bar.baz"