我有一个网页,其中有一个如下表格:
===========================================
Vehicle Num | GPS speed | GPS Location
===========================================
| |
| |
所以在document.ready()
之后,我正在为每个车辆行发起AJAX呼叫以获取相应车辆的GPS信息。这是我的代码:
var length = 47; // I have 47 vehicles
var vehicleNum = "";
var ajaxURL = "";
$('document').ready(function(){
for(var i = 1; i < length; i++)
{
vehicleNum = $("#"+i+"-vehicleNum").val();
//strip spaces from vehicle number
vehicleNum = vehicleNum.replace(/\s/g, '');
ajaxURL = "http://www.vehicletrack.biz/api/vehlastlocation?token=K2ZPJFJP3A&vehname="+vehicleNum;
//start ajax request
$.ajax({
url: ajaxURL,
dataType: "json",
success: function(data) {
if(data.result[0].error)
{
//error
}
else
{
// alert(i);
alert(i);
$("#"+i+"-speed").html(data.result[0].speed);
}
}
});
}
});
我正在为第47辆车更新速度值。什么应该是正确的方法(和优化的方法)?请提出任何建议。
修改 这是JSON响应之一
{"result":[{"longitude":76.391529,"latitude":27.974347,"location":"Delhi-Ajmer Expressway - Madhosinghpura- Alwar - Rajasthan - India","speed":0,"dttime":"14 Feb 2016 00:38:47","ignition":0,"vehicle_name":"HR38T7623","icon":0}]}