2012-04-01 47 views
0

當它能夠確定用戶的位置時,我得到了下面的JavaScript發佈數據。

if (document.cookie.indexOf("latLng") == -1) { 
    console.log("fired geolocation lookup"); 
    if (navigator.geolocation) { 
     navigator.geolocation.getCurrentPosition(success, error, {enableHighAccuracy:true,timeout:6000,maximumAge:2500}); 
    } else { 
     //html5 geolocation isn't supported in this browser 
     error('not supported'); 
     //render set location manually 
    } 
} else if (document.cookie.indexOf("latLng") == 0) { 
    $(function() { 
     organiseLocation(); 
    }); 

} 

function success(position) { 
    //cool we've got the location 
    //var locationData = {"location": position.coords.latitude+", "+position.coords.longitude, "radius": 50, "type": "auto"}; 
    console.log("position ", position); 
    $.ajax({ 
     type: "POST", 
     url: "../set_location", 
     data: position, 
     dataType: "json", 
     success: function(){ 
      console.log("success!"); 
     } 
    }); 
} 

function error(msg) { 
    //we couldn't determine your location 
    var s = document.querySelector('#status'); 
    s.innerHTML = typeof msg == 'string' ? msg : "failed"; 
    s.className = 'fail'; 
    //this needs cleaning up! 
    console.log(arguments); 
} 

它工作正常,在Chrome中,不過在Firefox & Safari中我碰到下面的錯誤 - 任何想法?

Illegal operation on WrappedNative prototype object 
[Break On This Error] 

value = jQuery.isFunction(value) ? value() : value; 

回答

1

它看起來像你需要使用JSON.stringify()序列化你的對象:

$.ajax({ 
    type: "POST", 
    url: "../set_location", 
    data: JSON.stringify(locationData), 
    dataType: "json", 
    success: function(){ 
     console.log("success!"); 
    } 
}); 

(編輯data:到字符串化的位置數據)

+0

的參數來通過爲{ 「所在地」=> {}}位置對象肯定有東西,因爲我已經註銷了它。有任何想法嗎? – Elliot 2012-04-01 19:08:36

+0

我編輯了代碼示例以序列化'locationData',這似乎是您感興趣的數據。基本上,您應該能夠將可序列化的任何內容串化並將其發送到服務器。 – slashingweapon 2012-04-01 22:01:45