miniprogramer--ajax

// api地址
const api = {
  book: '/todos/10'
}; 


function requestApi(url,params,method,sourceObj){
  if(method == 'POST'){
    var contentType = 'application/x-www-form-urlencoded'
  }else{
    var contentType = 'application/json'
  }
  return new Promise((resolve,reject)=>{
    wx.request({
      url: url,
      method:method,
      data:params,
      header: { 'Content-type': contentType},
      success:function(res){
        // typeof successFun == 'function' && successFun(res.data, sourceObj);
        resolve(res);
        console.log('resolve123456');
      },
      fail:function(res){
        // typeof failFun == 'function' && failFun(res.data,sourceObj)
        reject(res);
      },
      complete:function(res){
        // typeof completeFun == 'function' && completeFun(res.data,sourceObj)
        }
    })

  })
}

module.exports = {
  formatTime: formatTime,
  ajaxMethod: ajaxMethod,
  requestApi,
  api
}

  onShow:function(){
    console.log(app.globalData.webhost+""+utils.api.book)
    utils.requestApi(app.globalData.webhost + "" + utils.api.book, {}, this).then(e => {
      console.log(e, 456789123456789963);
      return new Promise(resolve=>{
        resolve(e);  //继续resolve传递下去
      })
    }).then((r)=>{
      console.log(r,456);
      utils.requestApi('https://jsonplaceholder.typicode.com/todos/1', {}, this).then(w=>{
        console.log(w,456789);
      })
    }).catch(e=>{console.log(e)})
  }

posted @ 2018-12-07 16:53  cyany_blue  阅读(154)  评论(0编辑  收藏  举报