I need to iterate through data that get via JSON with this code:
setInterval(function() {
console.log("running");
$.ajax({
type : 'get',
url : 'data.txt',
dataType : 'json',
success : function(response) {
console.log(response.bookings) // returns undefined
$.each(response.bookings, function(index, booking) {
sc.status(booking.seat_id, 'unavailable');
console.log(booking.seat_id); // returns nothing
});
}
});
}, 3000); //every 3 seconds
The data is currently formatted like this:
[{
"bookings": [
{
"seat_id": "1_4"
},
{
"seat_id": "4_2"
}]
}]
But that doesn't seem to be working. What is the correct format that I need to use? I tried a ton of options but can't get it working.
UPDATE:
The error I now get in the console is:
jquery-1.11.0.min.js:2 Uncaught TypeError: Cannot read property 'length' of undefined
at Function.each (jquery-1.11.0.min.js:2)
at Object.success ((index):145)
at j (jquery-1.11.0.min.js:2)
at Object.fireWith [as resolveWith] (jquery-1.11.0.min.js:2)
at x (jquery-1.11.0.min.js:4)
at XMLHttpRequest.b (jquery-1.11.0.min.js:4)
and line 145 is:
$.each(response.bookings, function(index, booking) {