【发布时间】:2019-11-25 11:13:43
【问题描述】:
我正在构建一个 react 应用程序(使用 create-react-app),它从使用 django 构建的服务器获取和发布数据。我正在将 axios 用于带有 react.js 的 API 请求。
它在开发模式下运行良好,但是当我构建 react 应用程序并部署到 netlify 时,请求 url 会发生变化。在开发阶段,Chrome 的开发者工具网络显示 Request URL: https://xyz.herokuapp.com/abc/ 但是当我构建应用程序并部署到 netlify 时,它显示 Request URL: https://loving-dijkstra-99eada.netlify.com/xyz.herokuapp.com/abc/ 并且 API 调用失败。 api调用的axios代码为:
axios.get('https:xyz.herokuapp.com/abc/')
.then((res) => {
console.log("successfully received the page count", res.data);
}).catch((err) => {
console.log(err, 'error in the get of data');
});
如何使它工作? package.json 在这里。
{
"name": "now-ui-kit-react",
"version": "1.0.0",
"private": true,
"dependencies": {
"antd": "^3.25.0",
"axios": "^0.19.0",
"moment": "2.24.0",
"node-sass": "4.12.0",
"nouislider": "14.0.2",
"react": "16.8.6",
"react-bootstrap-switch": "15.5.3",
"react-datetime": "2.16.3",
"react-dom": "16.8.6",
"react-router": "5.0.1",
"react-router-dom": "5.0.1",
"react-scripts": "3.0.1",
"reactstrap": "8.0.1",
"validator": "^12.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"install:clean": "rm -rf node_modules/ && rm -rf package-lock.json && npm install && npm start",
"compile-sass": "node-sass src/assets/scss/now-ui-kit.scss src/assets/css/now-ui-kit.css",
"minify-sass": "node-sass src/assets/scss/now-ui-kit.scss src/assets/css/now-ui-kit.min.css --output-style compressed",
"map-sass": "node-sass src/assets/scss/now-ui-kit.scss src/assets/css/now-ui-kit.css --source-map true"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"optionalDependencies": {
"typescript": "3.5.3"
}
}
【问题讨论】: