在作项目时请求接口需要从webserves中中转一下, 然鹅在node.js请求时拼接好的请求地址由中文导致出现Request path contains unescaped characters问题
- 刚开始时发现发送请求后,后端无法接收到发送的请求, 打印error发现了Request path contains unescaped characters问题
- 百度后发现有可能是请求地址中带有中文导致的请求无法发送,
- 解决: 在拼接好url后用encodeURI方法进行转码即可
encodeURI 和 decodeURI
- encodeURI可以将汉字进行解码
- decodeURI可以将解码后的汉字转回汉字
var str = '测试'
var str_utf8 = encodeURI(str)
console.log(str_utf8) // %E6%B5%8B%E8%AF%95
console.log(decodeURI(str_utf8)) // 测试