微信小程序promise封装get/post请求

1、utils文件夹下新建request.js并写入如下代码

function POST(url,params) {

let promise = new Promise(function(resolve, reject) {

wx.request({

url: url,

data: params,

header: {

"Content-Type": "application/x-www-form-urlencoded"

},

method: 'POST',

success: (res) => resolve(res),

fail: (res) => reject(res)

})

});

return promise;

}

 

function GET(url, params) {

let promise = new Promise(function(resolve, reject) {

wx.request({

url: url,

data: params,

header: {

"Content-Type": "application/x-www-form-urlencoded"

},

method: 'GET',

success: (res) => resolve(res),

fail: (res) => reject(res)

})

});

return promise

}

module.exports = {

POST: POST,

GET: GET,

}

 

2、使用

引入request模块:

var request = require('../../utils/request.js');

应用代码:

var params = {参数};

var url = "请求路径";

request.POST(url,params).then(res => {

this.setData({

})

})

你可能感兴趣的:(微信小程序request)