解决uniapp获取api的返回值丢失为Null

╰半橙微兮° 2021-09-07 17:46 1701阅读 0赞
  1. //使用getlocation这个api获取位置
  2. onShow() {
  3. //通过that 替换this,防止值的丢失
  4. let that = this;
  5. uni.getLocation({
  6. type: 'wgs84',
  7. geocode: true,
  8. success: function(res) {
  9. that.longitude = parseFloat(res.longitude);
  10. that.latitude = parseFloat(res.latitude);
  11. },
  12. });
  13. },
  14. //签到
  15. signIn() {
  16. if (this.tour_real_start === undefined) {
  17. this.$http.get('XXX接口名', {
  18. //这里通过this重新获取值,这个时候就能成功获取到res.longitude返回的值了
  19. jd: this.longitude, //精度+
  20. wd: this.latitude, //维度
  21. }).then(res => {
  22. if (res.data.result === 1) {
  23. uni.showToast({
  24. title: '签到成功',
  25. })
  26. } else {
  27. uni.showToast({
  28. title: res.data.msg,
  29. icon: 'none'
  30. })
  31. }
  32. })
  33. }
  34. },

发表评论

表情:
评论列表 (有 0 条评论,1701人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JackSon返回null字段

    说实话这个问题真的让我头疼,最终想到一个投机取巧的方法,下面且听我一一道来。 背景 因为mobile团队为了节省流量,所以架构组就把一些值为`null`或者`""`的字