微信小程序的请求request封装使用
下面是封装的请求接口在创建api/https.js
//get请求
function getRequest(url, data,response, error){
wx.request({
url: localhost,//后台地址
method: 'GET',
data: data,
header: {
// 'context-type': 'application/json'
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'arenaId':wx.getStorageSync('venue').id
},
success: res=> {
return response(res)
},
fail: err=> {
return error(err)
}
})
}
//post请求
function postRequest(url, data,response, error) {
wx.request({
url: localhost,//后台地址
method: 'POST',
data: data,
header: {
"content-type":"application/x-www-form-urlencoded;charset=utf-8",
'arenaId':wx.getStorageSync('venue').id
},
success: res=> {
return response(res)
},
fail: err=> {
return error(err)
}
})
}
//post传的josn对象
function postRequestBody(url, data,response, error) {
wx.request({
url: localhost,//后台地址
method: 'POST',
data: data,
header: {
'context-type': 'application/json',
'arenaId':wx.getStorageSync('venue').id
},
success: res=> {
return response(res)
},
fail: err=> {
return error(err)
}
})
}
export {
getRequest ,
postRequest,
postRequestBody
}
//使用在使用的js中引用
const http = require("../../api/https");
//get使用
http.getRequest("api/",{ statisDate:this.data.time },res=>{},err=>{ })
//post使用(json)
http.postRequestBody("api",jsonObject,res=>{},err=>{ })//jsonObject为对象
post使用
http.postRequest("api/",{ statisDate:this.data.time },res=>{},err=>{ })