我有一个latLng值的数组,想通过jQuery Ajax发布。我是如何转换数组的呢?
最好的问候......
答案 0 :(得分:1)
你可以这样做:
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'
})
当且仅当服务器期望发布变量数组coords
时。输出如下:
{coords: [{"latitude":"52.37253609047836","longitude":"4.868355474853502"},{"latitude":"52.37253609047836","longitude":"4.868355474853502"},{"latitude":"52.37253609047836","longitude":"4.868355474853502"}]}
答案 1 :(得分:1)
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");
});