I have a webpage where I have a table like following :
===========================================
Vehicle Num | GPS speed | GPS Location
===========================================
| |
| |
So after document.ready()
, I am firing AJAX call for each vehicle row to get the GPS info for corresponding vehicle. Here is my code :
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);
}
}
});
}
});
I am getting speed value updated only for the 47th vehicle. What should be the correct approach (and optimized one)? Any suggestions please.
EDIT This is one of the JSON response
{"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}]}