My proficiency in Javascript is definitely lacking, and I've only just begun to understand it.
I have set up an AJAX call to an API using the GET method. The JSON data returned by the API is pretty standard.
If I don't include an ID, I receive responses for ALL records. But when I do provide an ID, I get a SINGLE record response, which is what I expect.
Here's the code snippet:
$.ajax({
url: apiURL,
dataType: "json",
type: "Get",
success: function (data) {
var row;
for (var i = 0; i < data.length; i++) {
row = row + "<tr>";
row = row + "<td>" + data[i].id + "</td>";
row = row + "<td>" + data[i].latitude + "</td>";
row = row + "<td>" + data[i].longitude + "</td>";
row = row + "<td>" + data[i].description + "</td>";
row = row + "<td>" + data[i].dateRecorded + "</td>";
row = row + "</tr>";
};
$("#results").append(row);
......
After fetching the results, I want to display them in a table. Everything works smoothly when the response contains multiple records. However, if there's only a single record, I end up with an OBJECT instead of an ARRAY, causing the code to fail at data.length
. Why am I not getting an ARRAY with a single element and how can I address this issue?
Any guidance or assistance would be greatly appreciated.