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

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

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

 "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

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. uniapp H5 开发公众请求,proxy

    2024-03-28 11:34:07       44 阅读
  2. Ajax请求

    2024-03-28 11:34:07       36 阅读
  3. Angular: 配置 proxy 解决

    2024-03-28 11:34:07       53 阅读
  4. uniapp微信公众H5分享

    2024-03-28 11:34:07       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-28 11:34:07       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-28 11:34:07       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-28 11:34:07       87 阅读
  4. Python语言-面向对象

    2024-03-28 11:34:07       96 阅读

热门阅读

  1. Nginx服务

    2024-03-28 11:34:07       42 阅读
  2. Docker Compose 中的网络配置和优先级管理

    2024-03-28 11:34:07       44 阅读
  3. 无感刷新token

    2024-03-28 11:34:07       46 阅读
  4. CSS选择器 个人练习笔记

    2024-03-28 11:34:07       43 阅读
  5. 蓝桥杯-双指针

    2024-03-28 11:34:07       38 阅读
  6. JUC/多线程锁的用法(二)

    2024-03-28 11:34:07       36 阅读