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;