您的位置:首页 > 产品设计 > UI/UE

小程序不支持wx.request同步请求解决方法

2017-10-21 10:00 417 查看
小程序为了用户体验,所有的request均为异步请求,不会阻塞程序运行

百牛信息技术bainiu.ltd整理发布于博客园

所以当你需要同步请求,锁死操作时,最好将所有的逻辑写在success:function(){}

里面,

不然后出现返回值为空的尴尬

错误代码示例:



更改后的代码为:

onShow:function(){


// 页面显示




var commonFunction = require('../../pages/index/common'),


that = this;




var interval = setInterval(function(){


that.setData({


nowTime : commonFunction.formatTime(new Date())


})


},1000);


var request = function(latitude,longitude){


wx.request({


url: that.globalData.API_URL + 'getLocation',


data: {


latitude : latitude,


longitude : longitude


},


method: 'GET',


success: function(res){


let result = res.data.data;


result = JSON.parse(result);


console.log(result);


}


});


};


wx.getLocation({


"type" : 'gcj02',


"success" : function(res){


const latitude = res.latitude;


const longitude = res.longitude;


request(latitude,longitude);


},


"fail" : function(e){


console.log(e);


}


});








},






此文作用仅为填坑,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐