I got an array of latLng values and want to post this via jQuery Ajax. How did I have to convert the array?
Best re开发者_如何学Pythongards ...
You could do this:
var coords = ['(52.37253609047836, 4.868355474853502)', '(52.37253609047836, 4.868355474853502)', '(52.37253609047836, 4.868355474853502)'];
var coordConverted = [];
for (var i = 0; i < coords.length; i++) {
var values = coords[i].match(/\d+\.\d+/g);
coordConverted.push({
latitude: values[0],
longitude: values[1]
});
}
$.ajax({
type: 'POST',
data: {coords: coordConverted},
url: 'server'
})
If, and only if, the server is expecting post variable array coords
. The output looks like this:
{coords: [{"latitude":"52.37253609047836","longitude":"4.868355474853502"},{"latitude":"52.37253609047836","longitude":"4.868355474853502"},{"latitude":"52.37253609047836","longitude":"4.868355474853502"}]}
Joey was almost there.
lonLatArray = [....prepopulated...];
coords = [];
for(var i=0;i<latLonArray.length;i++){
coords.push({
lat: lonLatArray[i].lat(),
lon: lonLatArray[i].lon()
});
}
$.post("ajax.php",{data: coords},function(data){
console.log("success");
});
精彩评论