uniapp H5 开发,公众号时请求跨域了,要用proxy

这个要注意,如果你请求后端时,请求methed=OPTIONS 时,表示跨域了,这个是安全验证。

需要配置 {}/manifest.json 用代理模式

javascript 复制代码
 "h5" : {
        "router" : {
            "base" : "/h5",
            "mode" : "history"
        },
        "devServer" : {
            "disableHostCheck" : true,
            "proxy" : {
                "/api" : {
                    "target" : "http://localhost:8181/jdeps",
                    "changeOrigin" : true,
                    "ws" : true,
                    "pathRewrite" : {
                        "^/api" : ""
                    }
                }
            },
            "https" : false
        }
    }

request.js

javascript 复制代码
const ApiUrl = '/api' 

const request = (opt) => new Promise((resolve, reject) => {

	const token = uni.getStorageSync(tokenKey) || ""
	opt = opt || {};
	opt.url = opt.url || '';
	opt.data = opt.data || null;
	opt.method = opt.method || 'POST';
	opt.header = opt.header || {
		'content-type': 'application/x-www-form-urlencoded',
		'i18n': languageKeyMap[uni.getLocale()] || uni.getLocale(),
		"x-access-token": token,
		'wxno': wxno
	};
	uni.request({
		url: ApiUrl + opt.url,
		data: {
			...opt.data
		},
		method: opt.method,
		header: opt.header,
		dataType: 'json',
success: function(res) {
			const {
				data = {}
			} = res
			if (data.code == 200) {
				resolve(data.data);
			} else {
				uni.showToast({
					title: data.msg,
					duration: 2000,
					icon: 'none',
				})

				reject(data.msg);
			}
		},
		fail: function(e) {
			uni.showToast({
				title: e,
				duration: 2000,
				icon: 'none',
			})
			reject(e);
		}
	})
})

export default {
	request
}
相关推荐
街尾杂货店&1 天前
word属性解释
uni-app
笨笨狗吞噬者1 天前
【uniapp】小程序体积优化,JSON文件压缩
前端·微信小程序·uni-app
源码集结号1 天前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统
木易 士心2 天前
Uni-App 实现多身份动态切换 TabBar 指南
uni-app
2501_915106322 天前
CDN 可以实现 HTTPS 吗?实战要点、部署模式与真机验证流程
网络协议·http·ios·小程序·https·uni-app·iphone
LoveEate3 天前
uniapp 运行/发版微信小程序
微信小程序·小程序·uni-app
fakaifa4 天前
【高级版】沃德政务招商系统源码+uniapp小程序
小程序·uni-app·源码下载·沃德政务招商系统·招商系统源码
weixin_446938874 天前
uniapp vue-i18n如何使用
前端·vue.js·uni-app
有来技术4 天前
UniApp 自定义导航栏适配指南:微信小程序胶囊遮挡、H5 与 App 全端通用方案
微信小程序·uni-app
卷Java4 天前
违规通知功能修改说明
java·数据库·微信小程序·uni-app