微信小程序网络请求封装方法实例详细解读

why 64 2024-10-14

在这里首先声明一个小程序文档的bug,导致大伙们在请求的时候,服务器收到不到参数的问题
示例代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

wx.request({

 url: 'test.php', //仅为示例,并非真实的接口地址

 data: {

 x: '' ,

 y: ''

 },

 header: {

 'Content-Type': 'application/json'

 },

 success: function(res) {

 console.log(res.data)

 }

})


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

1

2

3

4

5

6

7

function json2Form(json) {

 var str = [];

 for(var p in json){

 str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));

 }

 return str.join("&");

}



图片:1.png

image.png

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


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

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

}


2、测试用例
2.1 get请求

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

//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 );

});


2.2 POST请求

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

//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 );

 });



图片:2.png

image.png

效果


图片:3.png

image.png



以上就是微信小程序网络请求的封装的详细内容。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序开发实现点击控件选中反选功能实例全解析
下一篇:小程序开发登录实例全面解读及步骤分析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~