收藏
回答

微信H5页面中调用JSSDK使用wx.getLocation控制台总是出错?

H5的JS文件部分代码如下

$(function(){
        $.ajax({
            url:'getWxConfig',
            type:'POST',
            data:{url:encodeURI(location.href)},
            dataType:'json',
            success:function(res){
                if(res.code > 0){ 
                    wx.config({
                        debug: false, 
                        appId: res.data.appId,
                        timestamp: res.data.timestamp,
                        nonceStr: res.data.nonceStr, 
                        signature: res.data.signature,
                        jsApiList: [
                            "getLocation"
                        ]
                    });
                    wx.error(function (res) {
                        console.log('JSSDK配置失败!' + res.errMsg)
                    })
                }else{
                   console.log(res.msg)
                }
            }
        })
    });
    wx.ready(function(){
        wx.getLocation({
            type:"gcj02",
            success:function(res){
                lat = res.latitude; 
                long = res.longitude;
                        console.log(lat); //从此行开始往下,开发工具控制台都不再打印和运行了
                        console.log(long);
            },
            cancel: function (res) {
                console.log('未授权位置获取请求');
            }
                  fail:function(err){
                    console.log(err); //开发者工具每次都是进到fail里,但是手机每次都是执行success里的代码,没办法调试下去了
                })
            });


以上代码在小程序开发工具运行,控制台最后一行总是出现

{errMsg: "getLocation:fail SyntaxError: Unexpected token u in JSON at position 0"}

不知道问题何在


最后一次编辑于  2023-08-14
回答关注问题邀请回答
收藏

2 个回答

  • 文潇
    文潇
    2023-09-06

    麻烦问一下解决了吗?我目前也是卡在这个问题

    2023-09-06
    有用
    回复
  • 黄同学
    黄同学
    2023-08-17

    问题就是开发者工具没法获取位置信息,或者说获取到的位置信息不是符合规范的json,所以一直报这个错误

    2023-08-17
    有用
    回复
登录 后发表内容