剛剛找到到通過creat-react-app創建的項目中跨域問題的解決方案,記錄下來以備后用。
如果接口地址為: http://my.example.com/eg-api 則配置package.json中的proxy代理服務器如下:
"proxy": { "/eg-api": { "target": "http://my.example.com", "changeOrigin": true, "secure": false } }
如果想調用多個不同域的接口,則配置如下:
"proxy": { "/eg-api01": { "target": "http://my.example.com", "changeOrigin": true, "secure": false }, "/eg-api02": { "target": "http://her.example.com", "changeOrigin": true, "secure": false } }
