1.路径中有未转义字符
UnhandledPromiseRejectionWarning: TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped
常见情况:路径中有中文字符。
characters
解决方法:
在JS中,使用encodeURI()方法转义字符后即可,再和原路径拼接。
//以下情况会报错
let searchWords = "植物"
let baseUrl = 'https://www.photocome.com/search?q='+searchWords ;
//解决方法
let str = encodeURI(searchWords) //转义后为unicode编码 %E5%89%A7%E6%83%85
let baseUrl = 'https://www.photocome.com/search?q='+str ;
//或者可以直接转义整个路劲
let str = encodeURI(baseUrl) //转义后为unicode编码 %E5%89%A7%E6%83%85
2.连接拒绝
UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:80
出现情况:axios的get请求路劲中没有添加http/https协议头,请求路径非法。
//以下情况会报错
axios.get('movie.douban.com/chart').then()
//axios的get必须要指明使用的应用层协议,才能发起合法的请求
axios.get('http://movie.douban.com/chart').then()