前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >uni-app学习笔记-请求接口跨域问题(八)

uni-app学习笔记-请求接口跨域问题(八)

作者头像
王小婷
发布2019-12-16 17:39:45
2.6K0
发布2019-12-16 17:39:45
举报
文章被收录于专栏:编程微刊编程微刊

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

服务器图片:http://www.intmote.com/star.png

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

代码语言:javascript
复制
methods: {
             getList() {
                uni.downloadFile({
                    url: 'http://www.intmote.com/star.png',
                    success: (res) => {
                       console.log(res);
                        this.imageSrc = res.tempFilePath;
                        uni.hideLoading();
                    },
                    fail: (err) => {
                        console.log('downloadFile fail, err is:', err)
                    }
                })
            }
        }

会发现控制台报错,遇到了跨域的问题了

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

想到之前写vue项目的时候,遇到过一次

https://cloud.tencent.com/developer/article/1423900

而uniapp是基于vue的,那么解决办法应该也是差不多的

解决办法:

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

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

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

代码语言:javascript
复制
 uni.downloadFile({
                    url: '/api/star.png',
                    success: (res) => {
                       console.log(res);
                        this.imageSrc = res.tempFilePath;
                        uni.hideLoading();
                    },
                    fail: (err) => {
                        console.log('downloadFile fail, err is:', err)
                    }
                })

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

代码语言:javascript
复制
<template>
    <view>     
        <view class="uni-padding-wrap uni-common-mt">
            <image class="img" v-if="imageSrc" :src="imageSrc" mode="center" />
        </view>
    </view>
</template>
<script>
    export default {
        data() {
            return {              
                imageSrc: ''
            }
        },
        onLoad() {
                   this.getList();
               },
        methods: {
             getList() {
                uni.downloadFile({
                    url: '/api/star.png',
                    success: (res) => {
                       console.log(res);
                        this.imageSrc = res.tempFilePath;
                        uni.hideLoading();
                    },
                    fail: (err) => {
                        console.log('downloadFile fail, err is:', err)
                    }
                })
            }
        }
    }
</script>
<style>
.img {
    width: 500upx;
    height: 500upx;
    margin: 0 95upx;
}
</style>

4:运行,跨域问题解决,图片就可以显示了

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 解决办法:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档