小程序开发网络请求封装教程详细解析

why 82 2024-10-21 11:05:34

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

1

2

3

4

5

6

7

8

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

function json2Form(json) {

 var str = [];

 for(var p in json){

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

 }

 return str.join("&"); }



最终还是认为是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

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

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

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


以上就是小程序开发之网络请求的封装教程的详细内容。


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

上一篇:小程序开发刻度尺滑块选择打赏金额功能详解
下一篇:小程序开发列表上拉加载下拉刷新效果实现教程
相关文章