今天實現了antd作為前端展現,python flask作為后端的數據填充,完全兩個獨立的服務;過程中遇到ajax跨越請求問題,導致status一直等於0,原來是這么寫的:
xmlhttp.open("GET", "http://192.168.118.129:8001/test_react_action?user_name=" + values.userName, true); xmlhttp.send();
被解析為跨越訪問了,這個時候需要在dva中添加代理才能訪問,方法如下:
修改.roadhogrc文件:
{ "entry": "src/index.js", "env": { "development": { "extraBabelPlugins": [ "dva-hmr", "transform-runtime", ["import", { "libraryName": "antd", "style": "css" }] ] }, "production": { "extraBabelPlugins": [ "transform-runtime" ] } }, "proxy": { "/api": { "target": "http://192.168.118.129:8001/", "changeOrigin": true, "pathRewrite": { "^/api" : "" } } } }
添加完紅色部分代碼以后,直接‘/api/test_react_action’就能訪問了。
xmlhttp.open("GET", "/api/test_react_action?user_name=" + values.userName, true); xmlhttp.send();
注意:如果同是本地的服務器,代理可能不能識別,我這192.168.118.129是單獨的一個服務,不在同一個ip。