uni-app請(qǐng)求接口跨域

需求:發(fā)起一個(gè)請(qǐng)求,請(qǐng)求到服務(wù)器上的json數(shù)據(jù),顯示在前端界面

服務(wù)器json數(shù)據(jù):http://www.xxx.com/test.json(個(gè)人域名不方便展示,用xxx代替隱藏)
數(shù)據(jù)是這樣的,直接可以在瀏覽器訪問(wèn)到。

寫(xiě)代碼:直接請(qǐng)求服務(wù)器接口的時(shí)候

<template>
    <view>
        <view class="cu-list menu-avatar">
            <view class="cu-item" v-for="(item,index) in productList" :key="index">
                <view class="">
                    <button v-if="item.isLeave === 0" @click.stop="isLeave()">離廠解綁</button>
                    <view v-else class="text-grey text-xs">{{item.leaveTime }}</view>
                </view>

            </view>
        </view>
    </view>
</template>
<script>
    export default {
        data() {
            return {
                productList: [],
            };
        },
        onLoad() {
            this.getList();
        },
        methods: {
            getList() {
                /* this.$request.get('/api/test.json', {
                    
                }).then(res => {                
                    // 打印調(diào)用成功回調(diào)
                    console.log(res)
                    this.productList = res.data.rows;
                }) */
                uni.request({
                    url: 'http://www.ixxx.com/test.json',
                    success: (res) => {
                        console.log(res.data);
                        this.itemList = res.data.rows;
                    }
                });
            },
        },



    }
</script>
<style>
    page {
        padding-top: 50px;
    }

    .header {
        position: absolute;
        top: 0;
        height: 44px;
        width: 100%;
        line-height: 50px;
        background-color: rgb(45, 47, 186);
        color: #fff;
    }

    .header text {
        display: inline-block;
        text-align: center;
    }

    .cu-bar.fixed,
    .nav.fixed {
        top: 45px;
    }

    .cu-list.menu-avatar>.cu-item {
        height: 92px;
        margin: 10px;
        box-shadow: 0 2px 5px -1px #c4c0c0;
    }
</style>

這里是出現(xiàn)了跨域問(wèn)題的哦


那么前端該怎么去解決跨域問(wèn)題?

想到之前寫(xiě)vue項(xiàng)目的時(shí)候,遇到過(guò)一次
http://www.itdecent.cn/p/b28cd8290b2a
而uniapp是基于vue的,那么解決辦法應(yīng)該也是差不多的

解決辦法:

1:打開(kāi)manifest.json文件,選擇源碼視圖,在里面添加proxy代理


      "devServer": {
                    "proxy": {
                        "/api": {                    
                            "target":"http://www.ixxxx.com",
                            "changeOrigin": true,//是否跨域
                            "secure": false,// 設(shè)置支持https協(xié)議的代理
                             "pathRewrite":{"^/api":"/"}
                        }
                    }
                },

2:回到當(dāng)前頁(yè)面,修改請(qǐng)求路徑

getList() {
                uni.request({
                    url: '/api/test.json',
                    success: (res) => {
                        console.log(res.data);
                    }
                });
            },

3:當(dāng)前頁(yè)面完整代碼(僅供參考)

<template>
    <view>
        <view class="cu-list menu-avatar">
            <view class="cu-item" v-for="(item,index) in productList" :key="index">
                <view class="">
                    <button v-if="item.isLeave === 0" @click.stop="isLeave()">離廠解綁</button>
                    <view v-else class="text-grey text-xs">{{item.leaveTime }}</view>
                </view>

            </view>
        </view>
    </view>
</template>
<script>
    export default {
        data() {
            return {
                productList: [],
            };
        },
        onLoad() {
            this.getList();
        },
        methods: {
            getList() {
                uni.request({
                    url: '/api/test.json',
                    success: (res) => {
                        console.log(res.data);
                    }
                });
            },
        },

    }
</script>
<style>
    page {
        padding-top: 50px;
    }

    .header {
        position: absolute;
        top: 0;
        height: 44px;
        width: 100%;
        line-height: 50px;
        background-color: rgb(45, 47, 186);
        color: #fff;
    }

    .header text {
        display: inline-block;
        text-align: center;
    }

    .cu-bar.fixed,
    .nav.fixed {
        top: 45px;
    }

    .cu-list.menu-avatar>.cu-item {
        height: 92px;
        margin: 10px;
        box-shadow: 0 2px 5px -1px #c4c0c0;
    }
</style>

4:運(yùn)行,跨域問(wèn)題解決,json數(shù)據(jù)請(qǐng)求到了,
就可以顯示了


這里說(shuō)的是在uni-app項(xiàng)目里面前端如何解決一下跨域問(wèn)題
渲染json數(shù)據(jù)的代碼就不寫(xiě)啦
代理路徑


圖片.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容