uniapp通过proxy解决跨域问题

//创建vite.config.js 文件

// 导入vite的配置对象
import {defineConfig} from "vite"
// 在uniapp环境下使用的vite 需要导入Dcloud的插件
import uni from "@dcloudio/vite-plugin-uni";

// 将配置导出
export default defineConfig({
	// 使用插件
	plugins: [
		uni()
	],
	// vite服务器配置
	server: {
		// 代码配置
		proxy: {
			// 源地址:https://m.douyu.com/api/home/mix
			// 识别信息
			'/bbdx': {
				// 源地址 域名或者 接口的公共部分
				target: 'https://api.book.bbdaxia.com',
				// 是否改变域名
				changeOrigin: true,
				// 路径重写  
				rewrite: path => {
					// 将识别标识再替换到  组合成完整的接口地址
					return path.replace(/^\/bbdx/, '')
				}
			},
			'/shop': {
				// 源地址 域名或者 接口的公共部分
				target: 'http://localhost:3000',
				// 是否改变域名
				changeOrigin: true,
				// 路径重写  
				rewrite: path => {
					// 将识别标识再替换到  组合成完整的接口地址
					return path.replace(/^\/shop/, '')
				}
			},
		}
	}
})
		

//页面发送请求 
//使用条件编译语句   如果是h5 发送跨域请求  如果是小程序发送完整url地址
     created() {
			console.log('created');
			// #ifndef H5
			const url = 'http://localhost:3000/shopping'
			// #endif
			// #ifdef H5
			const url = '/shop/shopping'
			// #endif
		
			uni.request({
				url: url,
				method: 'GET',
				data: {},
				success: res => {
					console.log(res);
				}
			});
		}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值