react中通过proxy解决跨域问题

一,对于使用creat-react-app构建的项目,可以直接在package.json下配置,具体如下

"proxy": "http://api.xxxx.com"

或者如果创建多个域,该如何设置proxy,能满足可以调用不同域下的API的需求?代码如下

//package.json中加入
  "proxy": {
    "/api/RoomApi": {
      "target": "http://open.douyucdn.cn",
      "changeOrigin":true
    },
    "/api/v1":{
      "target":"http://capi.douyucdn.cn",
      "changeOrigin":true
    }
  }

二,对于antd-pro的项目,需要在package.json的同等目录下添加.roadhogrc文件,具体代码:

{
  "entry": "src/index.js",
  "extraBabelPlugins": [
    "transform-runtime",
    "transform-decorators-legacy",
    "transform-class-properties",
    ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": true }]
  ],
  "env": {
    "development": {
      "extraBabelPlugins": [
        "dva-hmr"
      ]
    }
  },
  "externals": {
    "g2": "G2",
    "g-cloud": "Cloud",
    "g2-plugin-slider": "G2.Plugin.slider"
  },
  "ignoreMomentLocale": true,
  "theme": "./src/theme.js",
  "proxy": {
    "/api": {
      "target": "http://api.xxxx.com/",
      "changeOrigin": true
    }
  }
}




你可能感兴趣的:(前端,WEB)