小程序调用ajax方法,小程序调用ajax怎么弄
小程序开发的每一步都能够实现不一样的小程序功能,前段时间有朋友咨询小程序调用ajax方法,今天有空就为大家讲解一下,以下是调用小程序ajax的实现代码。
小编先下载了一个微信小程序的开发者工具,大概看了一下小程序开发文档,简单的实现了ajax请求。
头部标题和底部tab配置都在 app.json文件中,底部tab位最少两个,最多五个。下面是app.json文件代码和相关注释
[javascript] view plain copy{
"pages":[
"pages/index/index",
"pages/tucao/tucao",
"pages/center/center"
],
"window":{
"backgroundTextStyle":"",
"navigationBarBackgroundColor": "red",
"navigationBarTitleText": "一个标题而已",
"navigationBarTextStyle":"white"
},
"tabBar": {
"list": [{
"pagePath": "pages/index/index",
"text": "首页",
"iconPath": "/images/public/menu-cd.png",
"selectedIconPath": "/images/public/menu.png"
},{
"pagePath": "pages/tucao/tucao",
"text": "吐槽",
"iconPath": "/images/public/hot-cd.png",
"selectedIconPath": "/images/public/hot.png"
},{
"pagePath": "pages/center/center",
"text": "我的",
"iconPath": "/images/public/center-cd.png",
"selectedIconPath": "/images/public/center.png"
}],
"borderStyle": "white"
}
}
这里我是通过 微信小程序wx.request实现ajax请求服务器数据的,一个程序里面最多能有五个这样的请求。下面是index.js的代码
[javascript] view plain copy//index.js
//获取应用实例
var app = getApp()
Page({
data: {
motto: 'Hello World',
userInfo: {},
Industry:{}
},
onLoad: function (res) {
var that = this
//调用应用实例的方法获取全局数据
app.getUserInfo(function(userInfo){
//更新数据
that.setData({
userInfo:userInfo
})
})
wx.request({
url: 'xx.xxxxx/xxx.php',//上线的话必须是s,没有appId的本地请求貌似不受影响
data: {},
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// header: {}, // 设置请求的 header
success: function(res){
console.log(res.data.result)
that.setData({
Industry:res.data.result
})
},
fail: function() {
// fail
},
complete: function() {
// complete
}
})
}
})
其中小程序返回数据格式是一个json,格式如下:
展示页面就简单了,变量{{array}} 微信模版遍历数据使用 wx:for 。index.wxml代码如下:
[html] view plain copy
{{userInfo.nickName}}
{{index}}:{{item.name}}
参照以上的小程序代码,大家就可以实现小程序调用ajax了,怎么样,小程序调用ajax方法是不是很简单呢?大家都学会了吗?不懂的地方可以在文章底部留言,更多相关资料请关注微信小程序商店。
微信小程序调用方法,微信小程序接口调用方式实例
小程序template循环调用不了怎么办?
微信小程序js全局判断和js调用方法
上一篇:微信小程序下的用户心理营销策略
下一篇:如何进行小程序关键词申请占位