uniapp前端跨域设置

一、me.vue中

myClick(){
                uni.request({
                    // url: 'http://localhost:3000/api/list',
                    url: '/api/list',
                    method: 'GET',
                    data: {},
                    success: res => {
                        console.log(res);
                        this.list = res.data
                    },
                    fail: () => {},
                    complete: () => {}
                });
            }

二、manifest.json

 "h5" : {
        "title" : "h5",
        "devServer": {
            "proxy":{
                "/api":{
                    "target":"http://localhost:3000"
                }
            }
        }
    },

 特别注意:

h5 weixin 客户端判断,不同的url

myClick(){
                let url = ''
                // #ifdef H5
                url = '/api/list'
                // #endif
                // #ifdef MP-WEIXIN
                url = 'http://localhost:3000/api/list'
                // #endif
                
                uni.request({
                    url: url,
                    method: 'GET',
                    data: {},
                    success: res => {
                        console.log(res);
                        this.list = res.data
                    },
                    fail: (err) => {
                        console.log(err);
                    },
                    complete: () => {}
                });
            }

 升级一下

myClick(){
	let url = '/api/list'
// #ifndef H5 url = 'http://localhost:3000/api/list' // #endif uni.request({ url: url, method: 'GET', data: {}, success: res => { console.log(res); this.list = res.data }, fail: (err) => { console.log(err); }, complete: () => {} }); }

百度云token前端跨域应用

一、manifest.json

"h5" : {
        "title" : "h5",
        "devServer" : {
            "proxy" : {
                "/api" : {
                    "target" : "http://192.168.1.78:3000"
                },
                "/baiduyun" : {
                    "target" : "https://aip.baidubce.com/oauth/2.0/token?client_id=xxxxx......9k&client_secret=xxxx.....VC&grant_type=client_credentials", //目标接口域名
                    "changeOrigin": true, //是否跨域
                    "secure": false, // 设置支持https协议的代理,
                    "pathRewrite":{"^/baiduyun": ""}
                }
            }
        }
    },

二、scan.vue

uniapp_getbaidu_token(){
                let url = '/baiduyun/?client_id=xxx......9k&client_secret=xxxxxx......VC&grant_type=client_credentials'
                
                // #ifndef H5
                url = 'https://aip.baidubce.com/oauth/2.0/token?client_id=xxx......9k&client_secret=xxxx......VC&grant_type=client_credentials'
                // #endif
                uni.request({
                    url: url,
                    method: 'GET',
                    header:{
                        "Accept": "application/json",
                        "Content-Type": "application/json"
                    },
                    success: ({data}) => {
                        console.log(data.access_token);
                        this.baidu_token1 = data.access_token
                    },
                    fail: (err) => {
                        console.log(err);
                    },
                    complete: () => {}
                });
            },

 

posted on 2023-01-01 08:53  koolman  阅读(85)  评论(0)    收藏  举报