1. 程式人生 > 其它 >解決uni-app在H5端的跨域問題

解決uni-app在H5端的跨域問題

技術標籤:uni-appuni-app

問題報錯截圖:這是常見的前端瀏覽器跨域問題了
在這裡插入圖片描述

解決方法

修改 manifest.json 檔案,新增 H5 的跨域配置(使用 原始碼檢視開啟配置檔案)

// h5端跨域配置
"h5": {
	"devServer": {
		"disableHostCheck": true,
		"proxy": {
			"/api": {
				"target": "http://xxxxxxxx",
				"changeOrigin"
: true, "secure": false, "pathRewrite": { "^/api": "" } } } } }

如何請求

H5 端的請求地址字首改為 /api

uni.request({
	// #ifdef H5
	url: '/api/postclass',
	// #endif
	// #ifdef APP-PLUS
	url: this.config.webUrl + '/postclass',
	// #endif
	method: 'GET',
	success:
res => { console.log(res) }, fail: () => {}, });