Home jquery Unexpected end of input

Unexpected end of input

Author

Date

Category

Hello everyone. There is a code like this:

var getChat = setInterval (function () {
  $ .post ("/ chat.php? case = GetAll", function (data) {
    if (data! = "Error!") {
      var data = $ .parseJSON (data);
      $ ("# messages"). html ('');
      for (var i in data) {
        $ ("# messages"). append ('bla bla bla')
      }
    } else {
      alert (data);
    }
  });
  $ ("# messages"). scrollTop ($ ("# messages"). get (0) .scrollHeight);
}, 2000);

And here he actually, after several iterations (about 10-15), throws out the subject, the line indicates

var data = $ .parseJSON (data);

After that, the chat stops updating. I’m testing on LAN, so the problem is clearly not in blocking ip on the server

The script gives 10-20 array elements


Answer 1, authority 100%

Your data is empty

JSON.parse (''); // SyntaxError: Unexpected end of input

Add validation:

data = data! = ""? $ .parseJSON (data): {};

Answer 2

Here I found a solution to the problem, that my code was expecting a response from the server, and the server was silent after sending. So the error dropped out that the expected end of the json

fetch (url, {
    method: 'POST',
    body: JSON.stringify (json),
    headers: {
     'Content-Type': 'application / json'
    }
   })
    .then (res = & gt; res.json ()) // this is where my code is waiting for a response from the server
    .then ((res) = & gt; {
toastr.success ('Created Type is sent successfully');
    })
    .catch (err = & gt; {
     console.log ('Type send failed', err);
     toastr.warning ('Type send failed');
    })

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions