国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

微信小程序網絡請求的封裝與填坑之路

2024-05-06 16:36:20
字體:
來源:轉載
供稿:網友

以前寫過一篇關于微信小程序上拉加載,上拉刷新的文章,今天寫的是關于小程序網絡請求的封裝。

在這里首先聲明一個小程序文檔的bug,導致大伙們在請求的時候,服務器收到不到參數的問題

示例代碼:

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中,服務器不能收到,使用如下轉換之后

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請求的類

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請求

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

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

小程序封裝請求,微信小程序,網絡請求

效果

小程序封裝請求,微信小程序,網絡請求

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網!


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丹东市| 全州县| 大足县| 清新县| 阜平县| 保靖县| 泸州市| 电白县| 利川市| 惠来县| 龙门县| 高安市| 辽源市| 合阳县| 邛崃市| 都昌县| 石嘴山市| 鹿邑县| 行唐县| 乐都县| 吴江市| 古蔺县| 望谟县| 友谊县| 蕲春县| 安龙县| 宁陕县| 固阳县| 无为县| 长葛市| 郓城县| 东明县| 榆树市| 清水河县| 静乐县| 乃东县| 顺昌县| 庆城县| 涡阳县| 应城市| 文安县|