2012-04-01 75 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