react 設置代理(proxy) 實現跨域請求

一,對於使用creat-react-app構建的項目,能夠直接在package.json下配置,具體以下javascript

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

或者若是建立多個域,該如何設置proxy,能知足能夠調用不一樣域下的API的需求?代碼以下java

//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文件,具體代碼:react

{
  "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
    }
  }
}

.json

相關文章
相關標籤/搜索