react添加多个域名,跨域

Posted yinhao-jack

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react添加多个域名,跨域相关的知识,希望对你有一定的参考价值。

在package.json中加入如下:

 1 
 2   "name": "demo",
 3   "version": "0.1.0",
 4   "private": true,
 5   "proxy": 
 6     "/demo": 
 7       "secure": false,
 8       "target": "http://192.168.10.210:8080",
 9       "changeOrigin": true
10     ,
11 
12     "/TouTiao": 
13       "secure": false,
14       "target": "http://api.avatardata.cn",
15       "changeOrigin": true
16     ,
17     "/api": 
18     "secure": false,
19     "target": "http://dev.zxxk.com",
20     "changeOrigin": true
21     ,
22     "/ent": 
23       "secure": false,
24       "target": "https://interface.sina.cn",
25       "changeOrigin": true
26     ,"/mobile": 
27       "secure": false,
28       "target": "http://mini.eastday.com",
29       "changeOrigin": true
30     
31   ,
32   "dependencies": 
33     "antd": "^3.15.2",
34     "axios": "^0.18.0",
35     "http-proxy-middleware": "^0.19.1",
36     "jsonp": "^0.2.1",
37     "react": "^16.8.5",
38     "react-dom": "^16.8.5",
39     "react-router-dom": "^5.0.0",
40     "react-scripts": "1.1.1",
41     "react-swipe": "^6.0.4",
42     "swipe-js-iso": "^2.1.5"
43   ,
44   "scripts": 
45     "start": "react-scripts start",
46     "build": "react-scripts build",
47     "test": "react-scripts test",
48     "eject": "react-scripts eject"
49   ,
50   "eslintConfig": 
51     "extends": "react-app"
52   ,
53   "browserslist": [
54     ">0.2%",
55     "not dead",
56     "not ie <= 11",
57     "not op_mini all"
58   ]
59 

然后接口部分:
 1     getRuleAction()
 2         var self = this;
 3         return axios.post(/ent/feed.d.json?ch=health&col=ent&act=more&t=1484477669001&show_num=10&page=4‘).then((res) => 
 4             console.log("res",res.data.data)
 5             if (res.data.status == ‘1‘) 
 6                 let datalist=[]
 7                     this.setState(dataList:res.data.data);
 8             
 9         ).catch(function (error) 
10             console.log(error);
11         )
12 
13 
14     


接口处的、ent要和package.json中的 "/ent": 一直,便可成功访问

 

 

以上是关于react添加多个域名,跨域的主要内容,如果未能解决你的问题,请参考以下文章

IIS 如何设置多个Access-Control-Allow-Origin

nginx 多域名跨域

PHP跨域访问

React跨域

React跨域

服务器端PHP允许跨域