小程序ajax请求封装
小程序什么是小程序?什么是小程序?基于微信庞大的社交群体,可以为原生app导流创业公司优先推出小程序,开发成本低接近原生开发的用户体验特点:快速的加载能力更强大的能力接近原生的体验易用且安全的微信数据开发高效和简单的开发和普通页面的区别...
·
小程序
1、建basehost.js,配置请求的基础域名
export default {
doubanBaseHost: "https://douban.uieee.com",
localBaseHost: "localhost:8080/"
}
2、新建 ajax.js,封装wx.request
import basehost from './basehost.js';
class Base{
constructor(){}
//wx.request 封装方法 promise
request({url, params, method, hideLoading}){
return new Promise((resolve,reject)=>{
if(!hideLoading){
wx.showLoading({
mask:true,
title:'加载中...',
})
}
wx.request({
url: basehost.doubanBaseHost + url,
data: params,
method,//post get put delete option
header:{
'content-type':'json',
},
//成功
success(res){
resolve(res.data);
//if(res.code == 401){}
},
//失败
fail(err){
reject(err);
},
//完成
complete(){
wx.hideLoading();
}
});
})
}
}
export default Base;
3、新建api.js,封装api
import ajax from './ajax.js';
class Api extends ajax{
constructor(){
super();
}
//热门预告
filmTeaters(params){
return this.request({
method:'GET',
url:'/v2/movie/in_theaters',
params,
//hideLoading: true
})
}
}
export default new Api();
4、页面中使用
import Api from './../../utils/api.js'
Page({
data:{
theaters:{//热门电影
list: [],
total: 0
}
},
onLoad(options){
Api.filmTeaters({}).then(res=>{
this.setData({
theaters:{
list: res.subjects,
total: res.total,
}
});
console.log(res);
})
}
})

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)