uni-app请求接口跨域

简介: uni-app请求接口跨域

需求:发起一个请求,请求到服务器上的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数据的代码就不写啦

相关文章
|
2月前
|
Go 开发者
【应用服务 App Service】App Service发生错误请求时,如何查看IIS Freb日志,从中得知错误所发生的模块,请求中所携带的Header信息
【应用服务 App Service】App Service发生错误请求时,如何查看IIS Freb日志,从中得知错误所发生的模块,请求中所携带的Header信息
|
2月前
|
网络协议 安全 前端开发
【应用服务 App Service】Azure 应用服务测试网络访问其他域名及请求超时限制(4分钟 ≈ 230秒)
【应用服务 App Service】Azure 应用服务测试网络访问其他域名及请求超时限制(4分钟 ≈ 230秒)
|
2月前
|
API C#
【Azure App Service】验证App Service接受HTTP 2.0请求
【Azure App Service】验证App Service接受HTTP 2.0请求
|
2月前
|
API 持续交付 数据安全/隐私保护
【Azure ACR+App Service】ACR WebHook请求App Service时遇见 401 Unauthorized
【Azure ACR+App Service】ACR WebHook请求App Service时遇见 401 Unauthorized
|
2月前
|
API 网络架构 开发者
【Azure 应用服务】App Service多个部署槽(Slot)之间,设置Traffic百分比后,如何来判断请求是由那一个槽(Slot)来进行处理呢?
【Azure 应用服务】App Service多个部署槽(Slot)之间,设置Traffic百分比后,如何来判断请求是由那一个槽(Slot)来进行处理呢?
|
2月前
【Azure 应用服务】记一次 App Service 部分请求一直返回 401 "No Authority" 的情况
【Azure 应用服务】记一次 App Service 部分请求一直返回 401 "No Authority" 的情况
|
2月前
|
Web App开发 安全 JavaScript
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
|
2月前
|
网络协议 Linux Shell
【Azure 应用服务】App Service For Linux 中安装paping, 用于验证从App Service向外请求的网络连通性
【Azure 应用服务】App Service For Linux 中安装paping, 用于验证从App Service向外请求的网络连通性
|
2月前
|
SQL 网络协议 NoSQL
【Azure 应用服务】App Service/Azure Function的出站连接过多而引起了SNAT端口耗尽,导致一些新的请求出现超时错误(Timeout)
【Azure 应用服务】App Service/Azure Function的出站连接过多而引起了SNAT端口耗尽,导致一些新的请求出现超时错误(Timeout)
下一篇
无影云桌面