1.路径中有未转义字符

UnhandledPromiseRejectionWarning: TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped
常见情况:路径中有中文字符。
characters

解决方法:
在JS中,使用encodeURI()方法转义字符后即可,再和原路径拼接。

  1. //以下情况会报错
  2. let searchWords = "植物"
  3. let baseUrl = 'https://www.photocome.com/search?q='+searchWords ;
  4. //解决方法
  5. let str = encodeURI(searchWords) //转义后为unicode编码 %E5%89%A7%E6%83%85
  6. let baseUrl = 'https://www.photocome.com/search?q='+str ;
  7. //或者可以直接转义整个路劲
  8. 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协议头,请求路径非法。

  1. //以下情况会报错
  2. axios.get('movie.douban.com/chart').then()
  3. //axios的get必须要指明使用的应用层协议,才能发起合法的请求
  4. axios.get('http://movie.douban.com/chart').then()