uniapp 网络请求 get请求

网络请求

在uni中可以调用uni.request方法进行请求网络请求

需要注意的是:在小程序中网络相关的API在使用需要配置域名白名单。

官方文档

如果发起请求就调用我们这个uni.request(OBJECT)

发送get请求

<template>
    <div>
        <view>
            <button @click="sendReq">发送get请求</button>
        </view>
    </div>
</template>

这里的sendReq就是来配置发送请求的

里面定义一个方法

sendReq(){
    uni.request({     
	})
}

首先可以配置一个url

参数名类型必填默认值说明平台差异说明
urlString开发者服务器接口地址

这里就是我们要请求的接口地址

拿我这边随便接的地址

sendReq(){
    uni.request({
    	url:"http://localhost:8080/selectUser?id=0"
	})
}

还有一个最重要的:

参数名类型必填默认值说明平台差异说明
successFunction收到开发者服务器成功返回的回调函数

success就是我们请求成功后会触发的回调函数

里面的参数有一个res

sendReq(){
    uni.request({
    	url:"http://localhost:8080/selectUser?id=0",
        success(res){
            console.log(res)//打印一下我们回调函数传过来的值
        }
	})
}

其他可以参考官方文档

data:

要想从回调函数里面拿到传过来的值,那你必须得清楚this:

success()回调函数里的thisundefind

如果在回调函数里把能获取到的res.data的值给外部想进来的this.name是不太现实的(报错name is undefind)

但是在点击事件里,在uni.request外你去尝试一下输出一下:

console.log(this)

我这里出来的结果是VueComponent的实例对象,那我们把这个this赋给一个变量that让它进入回调函数里面把里面获取到的res.data.name(我数据库里面的是name,不清楚可以直接console.log(res)console.log(res.data)查看里面的值)

要知道VueComponent实例对象就是Vue实例对象的孪生兄弟中的弟弟,VueComponent实例对象对比Vue实例对象就是少一个确定为那个容器服务的el(或者说是$mount)和data只能写成函数式,所以Vue实例对象也是有_data的,里面存的就是组件中的data里的属性。所以我们只要在回调函数里面写成:

that._data.name = res.data.name

这样VueComponent里面的data就是传过来属性的值;

整体回调函数:

sendReq(){
    // console.log(this)//VueComponent实例对象
    let that = this
    uni.request({
        url:"http://localhost:8080/selectUser?id=0",
        success(res){
            console.log(res)
            console.log(res.data.name)
            //console.log(this)//undefined
            //console.log(that)//VueComponent实例对象
            that._data.name = res.data.name
        },
    })
}

这个案例的.vue文件

<template>
    <div>
        <view>
            <h2>名字:{{name}}</h2>
            <button @click="sendReq">发送get请求</button>
        </view>
    </div>
</template>

<script>
    export default{
        name: "req",
        components:{},
        data() {
            return {
                name: 'request拿到的名字'
            }
        },
        methods: {
            sendReq(){
                // console.log(this)//VueComponent实例对象
                let that = this
                uni.request({
                    url:"http://localhost:8080/selectUser?id=1",
                    success(res){
                        console.log(res)
                        console.log(res.data.name)
                        // text = res.data.name
                        //console.log(this)//undefined
                        //console.log(that)//VueComponent实例对象
                        that._data.name = res.data.name
                    },
                })
            }
        },
    }
</script>

<style scoped>

</style>
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

结城明日奈是我老婆

支持一下一直热爱程序的菜鸟吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值