- 代码
var header = {
'content-type': 'application/json',
'Authorization': wx.getStorageSync("token"),
'os': 'android',
'version': '1.0.0',
'device_token': 'ebc9f523e570ef14',
}
function post(url, params, onSuccess, onFailed) {
console.log("请求方式:", "POST")
request(url, params, "POST", onSuccess, onFailed);
}
function get(url, params, onSuccess, onFailed) {
console.log("请求方式:", "GET")
request(url, params, "GET", onSuccess, onFailed);
}
function request(url, params, method, onSuccess, onFailed) {
console.log('请求url:' + url);
wx.showLoading({
title: "正在加载中...",
})
console.log("请求头:", header)
wx.request({
url: url,
data: dealParams(params),
method: method,
header: header,
success: function(res) {
wx.hideLoading();
console.log('响应:', res.data);
if (res.data) {
if (res.statusCode == 200) {
onSuccess(res.data);
} else {
onFailed(res.data.message);
}
}
},
fail: function(error) {
onFailed("");
}
})
}
function dealParams(params) {
console.log("请求参数:", params)
return params;
}
module.exports = {
postRequest: post,
getRequest: get,
}
- 使用
引入:
var http = require('../../utils/httputils.js')
var prams = {
mobile: "1",
passWord:"1"
}
http.postRequest(baseUrl + 'api/app/users/userLogin' , prams,
function(res) {
},
function(err) {
}
)