uniapp代理跨域

1.manifest.json文件

H5配置中增加代理服务器

"h5": {
    
    
		"template": "template.h5.html",
		"router": {
    
    
			"mode": "history",
			"base": "./"
		},
		"devServer": {
    
    
			"port": 8080, //一般本地的端口为8080或8081
			"disableHostCheck": true, //默认配置,不用纠结
			"proxy": {
    
    
				"/api": {
    
       //这里是指所有请求url中包含api的都会走代理服务器
					"target": "https://baidu.com", //你的目标服务器域名,也就是真正请求的后端接口的根域名
					"changeOrigin": true, //host为变成target的值
					"secure": false,
					"ws": false,
					"pathRewrite": {
    
    
						"^/api": "" //路径重写,将api替换成空
					}
				}
			}
		}
	},
2.在api文件中
// #ifdef MP-WEIXIN
export const BASE_URL = 'https://baidu.com' //后台根域名
// #endif
// #ifdef H5
export const BASE_URL = '/api' //后台根域名  这样写,会给所有的接口都添加上/api,然后走代理服务器
// #endif

猜你喜欢

转载自blog.csdn.net/qq_41988896/article/details/134219522