美文网首页h5
uni-app请求接口跨域

uni-app请求接口跨域

作者: 祈澈菇凉 | 来源:发表于2022-03-03 16:46 被阅读0次

需求:发起一个请求,请求到服务器上的json数据,显示在前端界面

服务器json数据:http://www.xxx.com/test.json(个人域名不方便展示,用xxx代替隐藏)
数据是这样的,直接可以在浏览器访问到。

写代码:直接请求服务器接口的时候

<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 => {                
                    // 打印调用成功回调
                    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>

这里是出现了跨域问题的哦


那么前端该怎么去解决跨域问题?

想到之前写vue项目的时候,遇到过一次
https://www.jianshu.com/p/b28cd8290b2a
而uniapp是基于vue的,那么解决办法应该也是差不多的

解决办法:

1:打开manifest.json文件,选择源码视图,在里面添加proxy代理


      "devServer": {
                    "proxy": {
                        "/api": {                    
                            "target":"http://www.ixxxx.com",
                            "changeOrigin": true,//是否跨域
                            "secure": false,// 设置支持https协议的代理
                             "pathRewrite":{"^/api":"/"}
                        }
                    }
                },

2:回到当前页面,修改请求路径

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

3:当前页面完整代码(仅供参考)

<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:运行,跨域问题解决,json数据请求到了,
就可以显示了


这里说的是在uni-app项目里面前端如何解决一下跨域问题
渲染json数据的代码就不写啦
代理路径


图片.png

相关文章

  • uni-app请求接口跨域

    需求:发起一个请求,请求到服务器上的json数据,显示在前端界面 服务器json数据:http://www.xxx...

  • uni-app请求接口跨域

    需求:发起一个请求,请求到服务器上的json数据,显示在前端界面 服务器json数据:http://www.xxx...

  • uni-app 本地访问接口跨域

    uni-app 本地访问接口跨域[https://www.cnblogs.com/yeziyou/p/137402...

  • 使用CORS解决跨域共享

    1. 接口的跨域问题 刚才编写的 GET 和 POST接口,存在一个很严重的问题:不支持跨域请求。解决接口跨域问题...

  • vue简单接口封装 跨域问题处理

    vue接口封装: 第一步:解决跨域 接口请求,一般都会碰到跨域问题,在vue项目中,我们采用页面代理的方法解决跨域...

  • React 解决fetch跨域请求时session失效

    在解决fetch跨域请求接口的时候,一般都是让后台接口在返回头里添加 确实这样是可以解决跨域请求的问题,但是如果我...

  • AJAX出现两次请求 options和get|post

    跨域请求 允许跨域请求 preflighted request预请求(options) 跨域请求 XMLHttpR...

  • uni-app及vue浏览器跨域问题解决

    以猫眼电影接口为例: 假设请求接口 遇到跨域问题 vue解决跨域 在项目根目录下新建vue.config.js文件...

  • chrome浏览器设置--disable-web-securit

    在开发人员于后台进行接口测试的时候,老是遇到跨域问题,这时前端总是会让后台添加跨域请求头来允许跨域请求,今天介绍一...

  • Ajax 跨域请求

    如今大都要用到跨域的方法请求数据,网上的方法也是各式各样,这里我重点介绍两种不同的跨域形式。 一.跨域请求接口 主...

网友评论

    本文标题:uni-app请求接口跨域

    本文链接:https://www.haomeiwen.com/subject/kzkurrtx.html