测试接口

https://api.github.com/users

配置

  1. 正确访问路径:https://api.github.com/users
  2. // 请求
  3. fetch("/api/users")
  4. .then((res) => res.json())
  5. .then((res) => console.log(res));
  6. // 配置
  7. devServer: {
  8. proxy: {
  9. '/api': {
  10. target: 'https://api.github.com',
  11. pathRewrite: {'^/api': ''},
  12. changeOrigin: true,
  13. }
  14. }
  15. }

changeOrigin: true

这个配置项的作用是将请求的origin设置为target
原因:有些后端服务胡对origin做特别处理,不允许访问,从而报500错误,而 changeOrigin: true 可以将orgin设置为target指定的origin [ 可能有误 ]