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

小程序为了用户体验,所有的request均为异步请求,不会阻塞程序运行

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

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

里面,

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

错误代码示例:

更改后的代码为:

  1. onShow:function(){
  2. // 页面显示
  3. var commonFunction = require('../../pages/index/common'),
  4. that = this;
  5. var interval = setInterval(function(){
  6. that.setData({
  7. nowTime : commonFunction.formatTime(new Date())
  8. })
  9. },1000);
  10. var request = function(latitude,longitude){
  11. wx.request({
  12. url: that.globalData.API_URL + 'getLocation',
  13. data: {
  14. latitude : latitude,
  15. longitude : longitude
  16. },
  17. method: 'GET',
  18. success: function(res){
  19. let result = res.data.data;
  20. result = JSON.parse(result);
  21. console.log(result);
  22. }
  23. });
  24. };
  25. wx.getLocation({
  26. "type" : 'gcj02',
  27. "success" : function(res){
  28. const latitude = res.latitude;
  29. const longitude = res.longitude;
  30. request(latitude,longitude);
  31. },
  32. "fail" : function(e){
  33. console.log(e);
  34. }
  35. });
  36. },
  37. 此文作用仅为填坑,

转载于:https://www.cnblogs.com/bainiu/p/7703813.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值