1. Create a request.js file under utils, and then export the method
const app = getApp();
//Encapsulate request requests with promises
const POST = (url, params) => {
let promise = new Promise((resolve, reject) => {
wx.request({
url: url,
data: params,
header:'application/x-www-form-urlencoded span>', // Default value
method: "POST",
success: res => {
console.log("Return result:");
console.log(res.data);
app.netWorkData.result = res.data;
resolve();
},
fali: res => {
reject(res);
}
});
});
return promise;
};
module.exports = {
POST: POST
};
2, app.js
App({
netWorkData: {
result: {code: -1, msg: ‘Failed to initiate the request’}
},
})
3. First introduce the request.js file in the js file that requires the request data
const request = require('../../utils/request.js');
Call, send request
onLoad: function (options) {
const that = this;
var url = apiUrl + ‘api/info/querydetail‘;
var params={
"id": options.id, // id
"uid": "", // Current user id, you can leave it blank span>
"lat": lat, // Latitude The default is the dimension of Hangzhou
"lng": long,
}
network.POST(url, params).then(() => {
//Request a successful operation
var _data = app.netWorkData.result;
that.setData({
list: _data.data,
id: _data.data.id
});
});
},
const app = getApp();
//Encapsulate request requests with promises
const POST = (url, params) => {
let promise = new Promise((resolve, reject) => {
wx.request({
url: url,
data: params,
header:'application/x-www-form-urlencoded span>', // Default value
method: "POST",
success: res => {
console.log("Return result:");
console.log(res.data);
app.netWorkData.result = res.data;
resolve();
},
fali: res => {
reject(res);
}
});
});
return promise;
};
module.exports = {
POST: POST
};
App({
netWorkData: {
result: {code: -1, msg: ‘Failed to initiate the request’}
},
})
const request = require('../../utils/request.js');
p>
onLoad: function (options) {
const that = this;
var url = apiUrl + ‘api/info/querydetail‘;
var params={
"id": options.id, // id
"uid": "", // Current user id, you can leave it blank span>
"lat": lat, // Latitude The default is the dimension of Hangzhou
"lng": long,
}
network.POST(url, params).then(() => {
//Request a successful operation
var _data = app.netWorkData.result;
that.setData({
list: _data.data,
id: _data.data.id
});
});
},