uni.requet()封装

uni.requet()封装?

①uni-app提供了uni.requet()方法,发起网络请求;

③uni.request()未封装Vue组件中的使用;

③uni-app封装网络请求;


①uni-app提供了uni.requet()方法,发起网络请求:

uni.request({
    url: 'https://ceshi2.dishait.cn/api/v1/list', //仅为示例,并非真实接口地址。
    data: {
        text: 'uni.request'
    },
    header: {
        'custom-header': 'hello' //自定义请求头信息
    },
    success: (res) => {
        console.log(res.data);
        this.text = 'request success';
    }
});

  在各个小程序平台运行时,网络相关的 API 在使用前需要配置域名白名单。

data 数据说明最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下:

(1)对于get方法,会将数据转换为 query string。例如{ name: 'name', age: 18 } 转换后的结果是name=name&age=18。
(2)对于 post 方法且 header['content-type'] 为application/json的数据,会进行 JSON 序列化,是默认值。
(3)对于 post 方法且header['content-type']为 application/x-www-form-urlencoded 的数据,会将数据转换为 query string。

② uni.request()未封装Vue组件中的使用:

getList:function(){
    var _self=this;
    var token = uni.getStorageSync(_self.sessionKey);
    uni.request({
        url: 'https://ceshi2.dishait.cn/api/v1',
        method: 'GET',
        data: {
            token : token,
            timestamp : timestamp
                   },
        success: res => {
            if (res.data.code == "401") {//登录失效
                //弹框提示
                uni.showToast({
                    title: res.data.msg,
                    mask: false,
                    duration: 1500
                });
            } else if (res.data.code == "0") {
                var data = res.data.data;
                _self.onlineNum = data.onlineNum;
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值