Uniapp解决app和H5跨域问题

目录

 

1、创建request全局配置文件

2、在manifest.json中对H5进行配置

3、在main.js中全局配置

1、创建request全局配置文件

module.exports = (vm) => {
	// 初始化请求配置 
	
	// 设置跨域问题
	// #ifdef H5
	// 此为自定义配置参数,具体参数见上方说明
	uni.$u.http.setConfig({
		baseUrl: 'api',
		loadingText: '努力加载中~',
		loadingTime: 800,
		// ......
	});
	// #endif

// #ifndef H5
	// 此为自定义配置参数,具体参数见上方说明
	uni.$u.http.setConfig({
		baseUrl: 'http://192.168.xx.xx:xxxxx',
		loadingText: '努力加载中~',
		loadingTime: 800,
		// ......
	});
	// #endif

}

2、在manifest.json中对H5进行配置


	"h5": {
		"devServer": {
			"proxy": {
				"/api": {
					"target": "http://192.168.xx.xx:xxxx",
					"pathRewrite": {
						"^/api": ""//这个api要和request中对H5配置的baseUrl保持一致
					}
				}
			}
		}
	}

下面示例是文件所在位置

 3、在main.js中全局配置

import App from './App'
App.mpType = 'app'
const app = new Vue({
	...App
}) 

// 引入请求封装,将app参数传递到配置中
require('@/config/request.js')(app)

uniapp官网网络请求地址icon-default.png?t=M5H6https://uniapp.dcloud.io/api/request/request.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夢鑰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值