微信小程序-wx.request

微信在请求数据时提供wx.request(funciton)的方式获取数据

wx.request({
  url: 'test.php', //仅为示例,并非真实的接口地址
  data: {
     x: '' ,
     y: ''
  },
  header: {
      'content-type': 'application/json'
  },
  success: function(res) {
    console.log(res.data)
  }
})

 

其中的urlheadersuccessfail以及complete和普通的http请求是一样

参数和函数介绍看微信小程序开发api文档,这里不做详解

 

谢了一个封装好的回调函数

供以后用到时可以拿来用

/**
 * 封转wx.request的post提交请求方式,通过回调函数处理相关数据传递
 * @url:请求地址
 * @postData:请求参数
 * @doSuccess:请求成功回调函数
 * @doFail:请求失败回调函数
 * @doComlete:请求时执行函数
 */
function rewriteWXRequest(url,postData,doSuccess,doFail,doComplete){
  //console.log(url);
   wx.request({
    url: url,
    data:postData,
    method: 'POST', 
    success: function(res){
     if(typeof doSuccess == "function"){
       doSuccess(res);
     }
    },
    fail: function() {
     if(typeof doFail == "function"){
       doFail();
     }
    },
    complete: function() {
     if(typeof doComplete == "function"){
       doComplete();
     }
    }
   });
}

 

 

 微信其他相关上传,下载等问题:http://www.cnblogs.com/dragondean/p/5921088.html

 

posted @ 2017-01-12 17:16  243573295  阅读(1420)  评论(0编辑  收藏  举报