简体   繁体   中英

Posting object using $.ajax & jQuery

I've got the below JavaScript posting data when it is able to determine as users location.

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?

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:

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

(Edited data: to stringify the location data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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