7.配置代理解决ajax请求跨域问题【React antd】
在ajax请求接口中没有指定域名或者地址的话,默认为localhost
我们是希望从3000 端口 ———> 5000端口【其它任何提供服务的端口即可】访问获取数据出现 404即无法找到,因为我们3000端口的本地没有/login这个路径提供服务,所以报找不到
React + antd 解决办法如下:
1.jsonp :只能解决get的跨域
2.cors:让后端配置
3.配置代理:【推荐使用】【最简单有效的】【开发中使用,前提是用脚手架搭建的】
在package.json文件中加入如下代码即可: "proxy": "http://localhost:5000" 具体文件配置内容如下
{ "name": "react-admin-v5-pro", "version": "0.1.0", "private": true, "dependencies": { "@ant-design/icons": "^5.0.1", "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", "antd": "^5.2.2", "axios": "^1.3.3", "jsonp": "^0.2.1", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.8.1", "react-scripts": "5.0.1", "store": "^2.0.12", "web-vitals": "^2.1.4" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": [ "react-app", "react-app/jest" ] }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }, "proxy": "http://localhost:5000" }
整个项目已经开发完成,包括前后台,需要源码的可以联系: 微信号:guos_123 [添加请注明来意]