uniapp:解决跨域问题

458 阅读1分钟

通过配置proxy解决跨域

找到manifest.json文件-->源码透视

在下面加入

"h5": {
    "devServer": {
            "https": false,
            "proxy": {
                    "/api": {
                            "target": "http://xxx.com/", // 要请求的网站
                            "changeOrigin": true,
                            "secure": false,
                            "pathRewrite": {
                                    "^/api": "/"
                            }
                    }
            }
    }
}
复制代码

在页面uni.request请求中

 uni.request({
        url:'api/ajax/xxx',  //需要请求的地址
        data:{},
        success: (res) => {
                console.log(res);
        },
        fail: (err) => {
                console.log(err);
        }
 })
 
复制代码

就可以解决

image.png