小程序中ajax返回数据,微信小程序调用接口返回数据或提交数据
/*習(xí)慣用ajax了,則把(wx.request)封裝一下, 調(diào)用方式
1、先引入:const http = require('../../js/http.js')
2、使用方式:http.post或者h(yuǎn)ttp.get
3、params參數(shù)格式如:{ start: 1, count: 4}
*/
/*官方文檔https://mp.weixin.qq.com/debug/wxadoc/dev/api/network-request.html*/
module.exports = {
get(apiUrl, yes, error) {
wx.request({
url: apiUrl,
header: { 'Content-Type': 'json' },
success: yes,
fail: error
})
},
post(apiUrl, params, yes, error) {
wx.request({
url: apiUrl,
data: params,
header: { 'Content-Type': 'json' },
success: yes,
fail: error
})
}
}
2、頁(yè)面調(diào)用方式
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
const http = require('../../js/http.js')//引入http.js文件
var params = { start: 1, count: 4}; //參數(shù)
http.post("https://api.douban.com/v2/movie/coming_soon", { start:1,count:4}, function(res){
console.log("返回結(jié)果=" +JSON.stringify(res.data));
},function(e){
console.log("返回error結(jié)果=" + JSON.stringify(e));
} );
},
總結(jié)
以上是生活随笔為你收集整理的小程序中ajax返回数据,微信小程序调用接口返回数据或提交数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机视觉与图像处理面试题,深度学习图像
- 下一篇: 关于BigInteger的加减乘除使用