SyntaxError: JSON.parse: unexpected end of data in row 1 of column 1 of JSON data

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?

+8
source share
4 answers

Use the "dataType" parameter to identify the type of response, so calling .ajax () knows its JSON and no need to guess.

This may not solve the problem, since most likely your answer does not return JSON for the call that throws an error. If you add an error parameter, you will see that the server returns on error, but if the requests are completed, check the console for what is returned from the server. As I said, this is most likely not JSON if you get this error from $ .parseJSON () to start with.

 $(document).ready(function() { (function worker() { $.ajax({ url: 'server_info.json', dataType: 'json', success: function(data) { console.log(data); if (data.led_1 == "off") { // do stuff } if (data.led_2 == "off") { // do stuff } if (data.led_3 == "off") { // do stuff } }, error: function( data, status, error ) { console.log(data); console.log(status); console.log(error); } complete: function() { // Schedule the next request when the current one complete setTimeout(worker, 250); } }); })(); }); 
+5
source

Using Firefox debugging, I was able to see that the return type of the value was undefined when I get this error. Performing a check for null / undefined for the value that needs to be parsed first, then if the condition is false, continue to parse, otherwise process the condition, resolving my problem.

+1
source

After a long search for a solution to this problem and not finding a way to fix it, I tried to modify my mysql configuration file (my.ini) myself as follows. And it works!

The only thing I did was uncomment this paragraph:

 ## UTF 8 Settings init-connect=\'SET NAMES utf8\' collation_server=utf8_unicode_ci character_set_server=utf8 skip-character-set-client-handshake character_sets-dir="C:/xampp/mysql/share/charsets" 
0
source

Turns out the error has nothing to do with JSON. This is actually because the backend returns either JSON or an empty string.

 // Update if(updateFile($id,$author)) { echo json_encode( array('message' => 'Post Updated') ); } else { echo json_encode( array('message' => 'Post Not Updated') ); } 

For me, it worked. Good luck

0
source

Source: https://habr.com/ru/post/980620/


All Articles