UNIApp 跨域问题解决

2021-07-15  本文已影响0人  秋叶红90

注=== 仅支持本地调试使用,,,正式时候需要后台支持,如果同域名下面默认支持

准备两个hostUrl

var hostUrl = "https://xxxxx.com"
// 本地调试的时候
var isDebug = true
if (isDebug) {
    hostUrl = "/DebugApi"
}
function getApi(since_id,callBack){
    let url = hostUrl +  '/xx/xx'
    // since_id
    uni.request({
        url:url,
        data:{
            since_id:since_id
        },
        method:"POST",
        success: (res) => {
            console.log(res.data.data)
            callBack(res.data.data)
        }
    })
}



找到mainfest.json 源码打开粘贴进去即可


"h5" : {
        "title" : "",
        "router" : {
            "mode" : "hash"
        },
        "devServer" : {
            "port" : 8080, 
            "https" : false,
            "disableHostCheck" : true,
            "proxy": {  
                "/DebugApi": {  
                    "target": "https://xxxxx.com", // 你的目标接口域名
                    "changeOrigin": true,   
                    "secure": true,
                    "pathRewrite" : {
                        "^/DebugApi" : ""
                    }

                    
                } 
            }  
        }
    }

上一篇下一篇

猜你喜欢

热点阅读