使用$ .ajax&发布对象jQuery的

时间:2012-04-01 18:05:45

标签: javascript ruby-on-rails ruby-on-rails-3 jquery

当能够确定用户位置时,我得到了以下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 个答案:

答案 0 :(得分:1)

您似乎需要使用JSON.stringify()来序列化您的对象:

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

(编辑data:字符串化位置数据)