Promise encapsulates the WeChat applet Request

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');

 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
});

});

},

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2786 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.