Solution to the react cross-domain problem: First add [proxy": "http://localhost:8000] to [package.json]; then request [fetch('/api/userdata) in the page /')] will be forwarded to the address in the proxy.
Solution to react cross-domain problem:
1. The simplest operation
Add "proxy": "http://localhost:8000"
2. Add multiple proxies
Add to package.json"proxy": { "/api": { "target": "http://localhost:8000", "changeOrgin": true }, "/app": { "target": "http://localhost:8001", "changeOrgin": true } },
axios.post('/api/users').then(res =>{ console.log(res) })
3. Best recommendation
Download http-proxy-middlewarenpm i http-proxy-middleware --saveCreate src/setupProxy.jsconst proxy = require('http-proxy-middleware') module.exports = function(app) { // /api 表示代理路径 // target 表示目标服务器的地址 app.use( proxy('/api', { // http://localhost:4000/ 地址只是示例,实际地址以项目为准 target: 'http://localhost:4000', // 跨域时一般都设置该值 为 true changeOrigin: true, // 重写接口路由 pathRewrite: { '^/api': '' // 这样处理后,最终得到的接口路径为: http://localhost:8080/xxx } }) ) }
Related free learning recommendations:JavaScript (video)
The above is the detailed content of How to solve cross-react domain issues. For more information, please follow other related articles on the PHP Chinese website!