小程序网络访问的封装

来源:互联网 发布:图片上传java 编辑:程序博客网 时间:2024/05/24 02:38

以前写过一篇关于微信小程序上拉加载,上拉刷新的文章,今天写的是关于小程序网络请求的封装。 
在这里首先声明一个小程序文档的bug,导致大伙们在请求的时候,服务器收到不到参数的问题

示例代码:wx.request({  url: 'test.php', //仅为示例,并非真实的接口地址  data: {     x: '' ,     y: ''  },  header: {      'Content-Type': 'application/json'  },  success: function(res) {    console.log(res.data)  }})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

其中header 中的Content-Type,应该用小写content-type才能让服务器收到参数。让我折腾的好久,改了服务器仍然不行,原来是这个问题。参数在request payload中,服务器不能收到,使用如下转换之后

function json2Form(json) {      var str = [];      for(var p in json){          str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));      }      return str.join("&");  }  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里写图片描述

最终还是认为是content-type的问题。最后改小写就ok,觉得微信这么牛逼的团队,犯了一个很低级 的错误,把我开发者折腾的爬了。不说,上代码吧。

1 、Http请求的类

import util from 'util.js';/** * url 请求地址 * success 成功的回调 * fail 失败的回调 */function _get( url, success, fail ) {    console.log( "------start---_get----" );    wx.request( {        url: url,        header: {            // 'Content-Type': 'application/json'        },        success: function( res ) {            success( res );        },        fail: function( res ) {            fail( res );        }    });    console.log( "----end-----_get----" );}/** * url 请求地址 * success 成功的回调 * fail 失败的回调 */function _post_from(url,data, success, fail ) {     console.log( "----_post--start-------" );     wx.request( {        url: url,        header: {            'content-type': 'application/x-www-form-urlencoded',        },        method:'POST',        data:{data: data},        success: function( res ) {            success( res );        },        fail: function( res ) {            fail( res );        }    });     console.log( "----end-----_get----" );} /** * url 请求地址 * success 成功的回调 * fail 失败的回调 */function _post_json(url,data, success, fail ) {     console.log( "----_post--start-------" );    wx.request( {        url: url,        header: {            'content-type': 'application/json',        },        method:'POST',        data:data,        success: function( res ) {            success( res );        },        fail: function( res ) {            fail( res );        }    });    console.log( "----end----_post-----" );}module.exports = {    _get: _get,    _post:_post,    _post_json:_post_json}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

2、测试用例

2.1 get请求

   //GET方式    let map = new Map();    map.set( 'receiveId', '0010000022464' );    let d = json_util.mapToJson( util.tokenAndKo( map ) );    console.log( d );    var url1 = api.getBaseUrl() + 'SearchTaskByReceiveId?data='+d;    network_util._get( url1,d,      function( res ) {        console.log( res );        that.setData({          taskEntrys:res.data.taskEntrys        });      }, function( res ) {        console.log( res );      });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.2 POST请求

//Post方式 let map = new Map();    map.set( 'receiveId', '0010000022464' );    let d = json_util.mapToJson( util.tokenAndKo( map ) );    console.log( d );    var url1 = api.getBaseUrl() + 'SearchTaskByReceiveId';    network_util._post( url1,d,      function( res ) {        console.log( res );        that.setData({          taskEntrys:res.data.taskEntrys        });      }, function( res ) {        console.log( res );      });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

效果这里写图片描述

这里写图片描述

原创粉丝点击