0
点赞
收藏
分享

微信扫一扫

uni-app发送网络请求uni.request()。

芒果六斤半 2021-09-23 阅读 121
uni-app

案例模板:

url:是必须的,也就是我们的API接口。
header:请求头信息
method:请求方法,少量信息用get,大量数据、加密类用post
success:请求数据成功后的返回

        methods: {
            get:function(){
                //获取城市定位ID
                uni.request({
                    url:"https://geoapi.qweather.com/v2/city/lookup?location=shenzhen&key=4f27ec65289e43bab0a934a74f49f681",
                    method:"GET",
                    header:{
                        "content-type":"application/json"
                    },
                    success:function(res){
                        console.log(res)
                    }
                })
            }
        }

我这里使用API接口,是一个免费的天气类API接口,官网叫和风天气,大家可以去手动申请一个,人均每天1000次,测试用的话足够了。


当我们点击按钮发起请求后,就可以拿到数据了。



发送网络请求相关的面板在这里


此外uni.request()网络请求,不仅可以通过定义方法触发,也可以使用页面的生命周期来发送网络请求,。onLoad()和onShow()页面生命周期,当页面刷新的时候就请求数据进来(常用),其用法和methods同级。

        onShow() {
            uni.request({
                url:"https://geoapi.qweather.com/v2/city/lookup?location=shenzhen&key=4f27ec65289e43bab0a934a74f49f681",
                method:"GET",
                header:{
                    "content-type":"application/json"
                },
                success:function(res){
                    console.log(res)
                }
            })
        },
        methods: {
            get:function(){
                //获取城市定位ID
            }
        }

onLoad写法一样。

举报

相关推荐

0 条评论