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
}
相关推荐
小恒恒21 小时前
2025 Vibe Coding 有感
前端·uni-app·trae
一颗小青松1 天前
uniapp使用uni-im
uni-app
2501_916007471 天前
iPhone APP 性能测试怎么做,除了Instruments还有什么工具?
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 天前
Windows 环境下有哪些可用的 iOS 上架工具, iOS 上架工具的使用方式
android·ios·小程序·https·uni-app·iphone·webview
一颗小青松1 天前
uniapp vue3中app端使用腾讯云点播上传
uni-app·云计算·腾讯云
玄尺_0071 天前
uniapp h5端使浏览器弹出下载框
前端·javascript·uni-app
2501_915106321 天前
iOS 抓包工具有哪些?不同类型的抓包工具可以做什么
android·ios·小程序·https·uni-app·iphone·webview
web前端神器2 天前
vue、uniapp项目循环中能显示每个列表的内容,但是点击的时候传递的参数却不正确
uni-app
ModyQyW2 天前
HBuilderX 4.87 无法正常读取 macOS 环境配置的解决方案
前端·uni-app
脾气有点小暴2 天前
Uni-app App 端自定义导航栏完整实现指南
uni-app