I am making an AJAX request to my GPS server, which returns the following JSON format in case of success,
{"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":"ABCD","icon":0}]}
and in case of failure :
{"result":[{"error" : "Vehicle location did not found" }]}
This is my code :
ajaxURL = "http://www.vehicletrack.biz/api/vehlastlocation?token=K2ZFKFMP3A&vehname="+vehicleNum;
//start ajax request
$.ajax({
url: ajaxURL,
dataType: "text",
success: function(data) {
alert(data[0].longitude);
}
});
Firstly, I want to handle the error case and if result is proper then I want to display each field. Can anyone please help me with accessing the elements of JSON response and handle the error case.
PS : This is my first time with JSON and googling didn'r return relevant JSON format example. Hit and Trial didn't help.
data.result[0].longitude$.parseJSON(data);inside your success handler?