简体   繁体   English

使用$ .ajax和jQuery发布对象

[英]Posting object using $.ajax & jQuery

I've got the below JavaScript posting data when it is able to determine as users location. 当能够确定用户位置时,我已经获得了以下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);
}

It works fine in Chrome, however in Firefox & Safari I get the following error - any ideas? 它在Chrome中正常运行,但是在Firefox和Safari中,我收到以下错误-有任何想法吗?

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

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

It looks like you need to use JSON.stringify() to serialize your object: 看来您需要使用JSON.stringify()来序列化您的对象:

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

(Edited data: to stringify the location data) (编辑data:对位置数据进行字符串化处理)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM