1.请求示例A
// 加载信息
loadInfo: function() {
let that = this
let url = 'https://api.apiopen.top/getJoke?page=1&count=10'
wx.request({
url: url,
method: 'GET',
dataType: 'json',
header: {
'content-type': 'application/json' // 默认值
},
success: function(res) {
console.log(res.data)
},
fail: function(error) {
console.log('操作失败')
}
})
},
- 请求示例B
// 加载信息
loadInfo: function() {
let that = this
wx.request({
url: 'https://api.apiopen.top/getJoke',
data: {
// 页码
page: '1',
// 每页返回数量
count: '10',
type: ''
},
method: 'GET',
dataType: 'json',
header: {
'content-type': 'application/json' // 默认值
},
success: function(res) {
console.log(res.data)
},
fail: function(error) {
console.log('操作失败')
}
})
},
- Promise的使用
// 加载信息
loadInfo: function () {
let that = this
let promise = new Promise((resolve, reject) => {
wx.request({
url:'https://api.apiopen.top/getJoke',
data:{
page:'1',
// 每页返回数量
count:'10',
type:''
},
method: 'GET',
dataType: 'json',
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
resolve(res.data)
console.log(res.data)
},
fail: function (error) {
reject(error)
console.log('操作失败')
}
})
})
return promise
},
- 注意:
url: 'https://api.apiopen.top/getJoke?page=1&count=10'
等价于
url: 'https://api.apiopen.top/getJoke',
data: {
// 页码
page: '1',
// 每页返回数量
count: '10',
type: ''
},