zoukankan      html  css  js  c++  java
  • 钉钉小程序封装网络请求

    1.在最外层的app.js中

     1 App({
     2   globaldata: {
     3     serverurl:'实际地址', 
     4     // serverurl: 'http://172.16.1.47:7001',
     5   },
     6   //由于接口的传参方式不同(可能是formData形式,也可能是query形式),因此将此条件也封装了进去
     7   // 封装网络请求
     8   https(httpstype, url, data , ContentType) {
     9     dd.showLoading();
    10     let endurl = encodeURI(this.globaldata.serverurl + url);
    11     return new Promise((resolve, reject) => {
    12       dd.httpRequest({
    13         headers: {
    14           "Content-Type": ContentType ? ContentType : 'application/x-www-form-urlencoded'
    15         },
    16         url: endurl,
    17         method: httpstype,
    18         // 需要手动调用JSON.stringify将数据进行序列化
    19         data: data,
    20         dataType: 'json',
    21         success: function(res) {
    22           resolve(res.data)
    23         },
    24         fail: function(res) {
    25           reject(res)
    26         },
    27         complete: function(res) {
    28           dd.hideLoading()
    29         }
    30       });
    31     })
    32   },
    33 });
    34  

    2.使用请求,在组件中请求数据

     1 let app = getApp()
     2  
     3 Component({
     4   methods: {
     5     getData() {
     6       let params = {
     7            id:188
     8       }
     9       app.https('get','/wxMicroRecruitment/share/positionDetails',params).then(res => {
    10         console.log(res)
    11       })
    12     },
    13   },
    14 })

      

  • 相关阅读:
    J2SE基础:7.系统经常使用类一
    Win7 64位 php-5.5.13+Apache 2.4.9+mysql-5.6.19 配置
    FUDCon
    扬帆起航 彼岸花开——2013届毕业晚会《再见民大》倾情再演
    毛磊
    查经
    H.O.T candy
    svn rm --keep-local ./QueryParser_kill.logs
    python datetime笔记
    柯震东_百度百科
  • 原文地址:https://www.cnblogs.com/dreamstartplace/p/14507451.html
Copyright © 2011-2022 走看看