48

node.js含有%百分号时,发送get请求时浏览器地址自动编码的问题

 4 years ago
source link: https://www.tuicool.com/articles/mYR7vem
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

目前浏览器会对地址,进行编码,比如这个文件名:

sales\报表.png

在发到后台时,会自动编码成:

/file/view/sales/%E6%8A%A5%E8%A1%A8.png

在node.js会自动反编码成:

sales\报表.png

不过如果文件名中含有%百分号,编码过程则会出现问题,如

sales\报%表.png

发送到node.js接收到的自动编码数据:

/file/view/sales/%E6%8A%A5%%E8%A1%A8.png

%E6%8A%A5 % %E8%A1%A8.png 因为中间的百分号没有自动编码,然后导致node.js自动解码失败。

解决方案

含有百分号时,先在前端手动编码

encodeURIComponent('报%表.png')

此时%会编码成%25,

%E6%8A%A5<b>%25</b>%E8%A1%A8.png

发送到后台,解码正常。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK