Home > Backend Development > PHP Tutorial > javascript - How to obtain longitude and latitude through WeChat js and how to receive it in the background

javascript - How to obtain longitude and latitude through WeChat js and how to receive it in the background

WBOY
Release: 2023-03-03 08:34:02
Original
1900 people have browsed it

wx.getLocation({

<code>    type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
    success: function (res) {
        var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
        var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
        var speed = res.speed; // 速度,以米/每秒计
        var accuracy = res.accuracy; // 位置精度
        
        $.ajax({
            url:"{:U('home/Share/index')}",
            data:{latitude:latitude,longitude:longitude},
            type:"POST",
            dataType:"json",
            success:function(){
                
            }
        })
    },
})</code>
Copy after login
Copy after login

When using ajax to transfer values ​​to the background controller, var_dump($_POST) is a null value. What’s going on?

Reply content:

wx.getLocation({

<code>    type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
    success: function (res) {
        var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90
        var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。
        var speed = res.speed; // 速度,以米/每秒计
        var accuracy = res.accuracy; // 位置精度
        
        $.ajax({
            url:"{:U('home/Share/index')}",
            data:{latitude:latitude,longitude:longitude},
            type:"POST",
            dataType:"json",
            success:function(){
                
            }
        })
    },
})</code>
Copy after login
Copy after login

When using ajax to transfer values ​​to the background controller, var_dump($_POST) is a null value. What’s going on?

The writing is correct. As for why you can’t get it, you need to debug your code yourself.


Meat testing method:

Output the received data directly, and then accept the returned object in success.

<code>后台输出:
echo json_encode($_POST);exit();
前台输出:
success:function(msg){
        console.log(msg);    
       }</code>
Copy after login

ajax is an asynchronous request and cannot be output directly in the controller

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template