promise使用promise进行封装http请求接口
生活随笔
收集整理的這篇文章主要介紹了
promise使用promise进行封装http请求接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
封裝?
// 網絡請求http(options) {const app = getApp();let urlTest = 'https://businessapi.test.bajiechewu.com/'; // 測試地址let url = 'https://businessapi.chequan.cn/'; // 正式地址return new Promise((resolve, reject) => {my.request({url: urlTest + options.url,method: options.method || "POST",data: {token: app.globalData['token'],...options.data},headers: {'content-type': 'application/json', //默認值...options.headers},dataType: 'json',success: (result) => {resolve(result);},fail: (res) => {reject(res);}});})}?調用
app.http({url: 'licensescore/getauth',headers: {'code': res.authCode}}).then(res => {if (res.data.code == 1) {my.setStorage({key: 'token',data: {token: res.data.data.token},success() {app.globalData['token'] = res.data.data.token;_this.getData();}});}}).catch(error => {console.log(error);})?
總結
以上是生活随笔為你收集整理的promise使用promise进行封装http请求接口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 糖醋带鱼家常做法?
- 下一篇: JS的ES6模板字符串(拼接、变量、循环