react-antd-admin icon indicating copy to clipboard operation
react-antd-admin copied to clipboard

如何跨域?

Open yinsang opened this issue 8 years ago • 2 comments

我java后台是在本地跑的,返回一个json。localhost:8080 我把react-antd-admin设置为localhost:9090,想访问localhost:8080/bpm/pi时。 1,我在webpack.config.js中加入proxy怎么代理不过去?还是显示在localhost:9090/bpm/pi; 2,为什么我在config.js的api中修改host,能跨域到localhost:8080,但是返回: XMLHttpRequest cannot load http://localhost:8080/bpm/pi. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9090' is therefore not allowed access. 这个项目上线的话是不需要跨域的,所以不能修改java代码。我改怎么改?

yinsang avatar Aug 09 '17 10:08 yinsang

对于这种情况,暂时没什么好办法😓

  1. webpack.config.js中的proxy配置,不是很了解。。
  2. 通过修改config.js中的host,确实能实现跨域的,但需要后端接口支持。我们一般的做法是将后端接口的跨域做成可配置项,只在测试/开发时开启。或者使用使用一些白名单机制之类的。

这个我再想想,看下有啥办法能简化下开发

jiangxy avatar Aug 10 '17 02:08 jiangxy

直接在webpack中配置 非常简单

devServer: {
    "proxy": {
      "/login": {
        "target": "http://localhost:8080",
      },
      "/api/*": {
        "target": "http://localhost:8080",
      }
    }
  }

super-ai avatar Aug 18 '17 08:08 super-ai