Ok, so I am trying to extract data from a json file that contains the status of several LEDs, etc. I have a script that runs several times per second and retrieves data, and the webpage loads it. The problem is that after about 20 times the server reads the json file, in the end it will throw this error.
SyntaxError: JSON.parse: unexpected end of data in row 1 of column 1 of JSON data
// For toggling the LED/switch status indicators using the json data $(document).ready(function() { (function worker() { $.ajax({ url: 'server_info.json', success: function(data) { var json = $.parseJSON(data); console.log(json); if (json.led_1 == "off") { // do stuff } if (json.led_2 == "off") { // do stuff } if (json.led_3 == "off") { // do stuff } }, complete: function() { // Schedule the next request when the current one complete setTimeout(worker, 250); } }); })(); });
The json file is as follows:
{ "led_1": "on", "led_2": "on", "led_3": "on" }
It seems to me that json data is always properly formatted. I do not understand where the error comes from. Any ideas?
source share