小程序接口调用

外部因素决定了----post文件的大小有限制,而get没有。 (提交数据的大小)
get将提交的数据放到url后面,post将数据放到请求头中 (提交数据的位置)
在一次数据的提交中,get提交的数据任何人可见,post提交的数据除本人或黑客拦截请求数据包外,其他人不可见。

get
{
console.log(‘开始请求数据’,new Date());
wx.request({
// url: app.ctURL + ‘api/region’,
url:‘http://datavmap-public.oss-cn-hangzhou.aliyuncs.com/areas/csv/100000_province.json’,
method: “GET”,
header: {
‘Content-Type’: ‘application/x-www-form-urlencoded’
},
success: function (res) {
console.log(‘请求完成’, new Date());
_this.setData({
subjects: res.data.data,//将数据存入subject
})
console.log(res);
},
// 失败
fail(res){
console.log(‘失败’);
}
})
}

post
{
console.log(‘开始请求数据’,new Date());
wx.request({
url: app.baseURL + ‘/wx/product/all_banner’,
// url:‘https://api.inno-we.cn//wx/product/all_banner’,
method: “POST”,
success: function (res) {
console.log(‘请求轮播图完成’, new Date());
console.log(res.data.data);
_this.setData({
banner: res.data.data,
})
// console.log(_this.data.banner);
}
})
}

固定格式
onLoad: function (options) {
var that = this;
wx.request({
url: ‘接口’,
method: ‘GET’,//方法分GET和POST,根据需要写
header: {//定死的格式,不用改,照敲就好
‘Content-Type’: ‘application/json’
},
success: function (res) {//这里写调用接口成功之后所运行的函数
console.log(res.data);//调出来的数据在控制台显示,方便查看
that.setData({
resdata: res.data,//res.data就是你调出来的所有数据(当然也可以在这里面自定义想要调用的数据),然后把值赋给resdata,之后对resdata进行处理即可,具体见wxml
})
},
fail: function (res) {//这里写调用接口失败之后所运行的函数
console.log(’…fail…’);
}
})
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值