首頁  >  文章  >  後端開發  >  javascript - 微信js取得經緯度如何傳值以及後台如何接收

javascript - 微信js取得經緯度如何傳值以及後台如何接收

WBOY
WBOY原創
2016-12-05 13:44:101688瀏覽

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>

這樣用ajax傳值在後台controller,var_dump($_POST)是空值,是怎麼回事?

回覆內容:

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>

這樣用ajax傳值在後台controller,var_dump($_POST)是空值,是怎麼回事?

寫法是正確的,至於為什麼獲取不到就需要自己調試一下你的程式碼。


肉測辦法:

直接輸出接受到的數據,然後success中接受回傳的物件。

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

ajax是屬於非同步請求,不能直接在controller輸出

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn