[debugging] Chrome: Uncaught SyntaxError: Unexpected end of input

When loading my page in Google Chrome, I get a vague error in the console:

Uncaught SyntaxError: Unexpected end of input

I have no idea what is causing it. How would I go about debugging this error?

This question is related to debugging google-chrome syntax-error

The answer is


JSHint is good at finding the location of missing brackets or bad syntax.


In my case I was adding javascript dynamicly and using double quotes 2 times in string templates so i changed the second to single quotes and the error was gone. I hope it will help some of the people coming here for the same reason.


I get this error when I have ommitted a closing brace character (})in JavaScript code. Check that your braces are properly balanced.


My problem was with Google Chrome cache. I tested this by running my web application on Firefox and I didn't got that error there. So then I decided trying emptying cache of Google Chrome and it worked.


Since it's an async operation the onreadystatechange may happen before the value has loaded in the responseText, try using a window.setTimeout(function () { JSON.parse(xhr.responseText); }, 1000); to see if the error persists? BOL


if you got error at Anchor tag,just replace "Onclick" with "href" or "href" with "Onclick"


In my case, i had low internet speed, when i turn off the other user's internet connection then error has gone, strange


Trying to parse an empty JSON can be the cause of this error.

When you receive a response from the server or whatever, check first if it's not empty. For example:

function parseJSON(response) {
    if (response.status === 204 || response.status === 205) {
        return null;
    }
    return response.json();
}

Then you can fetch with:

fetch(url, options).then(parseJSON); 

In cases where your JavaScript code is minified to a single line, another cause for this error is using // instead of /**/ for your comments.

Bad (comments out everything after // including the closing } for your function)

function example() { //comment console.log('TEST'); }

Good (confines your comment)

function example() { /* comment */ console.log('TEST'); }

Setting the Accept header to application/json in the request worked for me when I faced the same problem.


For the record, for anyone trying to find the various causes for this error. An empty HTML5 data attribute

data-mydata = ''

causes the error too. You should check when the data value is a null string and not include the attribute at all. It goes without saying this is largely relevant to script generated HTML.


Another cause of this error: if your API intentionally responds with no response body, but responds with a 200 OK status code instead of a 204 No Content status code. Some JavaScript libraries may not respond well to unexpected content types when there is no content, so use the correct status code!


Try Firebug for Mozilla - it will show the position of the missing }.

http://getfirebug.com/


I faced similar problem using ui bootstrap directive for angularjs - uib-datepicker, when pressing am/pm toggle.

Error in event handler for (unknown): SyntaxError: Unexpected end of JSON input angular timepicker

Turned out it was because of plugin 'Trans-over' (which translates a word when it is clicked). Maybe my answer will help someone, because I didn't found anything on the internet.


The issue for me was that I was doing $.ajax with dataType: "json" for a POST request that was returning an HTTP 201 (created) and no request body. The fix was to simply remove that key/value.


There will definitely be an open bracket which caused the error.

I'd suggest that you open the page in Firefox, then open Firebug and check the console – it'll show the missing symbol.

Example screenshot:

Firebug highlighting the error


I had this error and fixed it by adding the guard on readyState and status shown here:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
          // Your code here
   }
};

See my case on another similar question:

In my case, I was trying to parse an empty JSON:

JSON.parse(stringifiedJSON);

In other words, what happened was the following:

JSON.parse("");

Examples related to debugging

How do I enable logging for Spring Security? How to run or debug php on Visual Studio Code (VSCode) How do you debug React Native? How do I debug "Error: spawn ENOENT" on node.js? How can I inspect the file system of a failed `docker build`? Swift: print() vs println() vs NSLog() JavaScript console.log causes error: "Synchronous XMLHttpRequest on the main thread is deprecated..." How to debug Spring Boot application with Eclipse? Unfortunately MyApp has stopped. How can I solve this? 500 internal server error, how to debug

Examples related to google-chrome

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 SameSite warning Chrome 77 What's the net::ERR_HTTP2_PROTOCOL_ERROR about? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium Jupyter Notebook not saving: '_xsrf' argument missing from post How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser How to make audio autoplay on chrome How to handle "Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first." on Desktop with Chrome 66?

Examples related to syntax-error

Uncaught SyntaxError: Unexpected token u in JSON at position 0 How to solve SyntaxError on autogenerated manage.py? Checking whether the pip is installed? (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape How can you print multiple variables inside a string using printf? Unexpected token < in first line of HTML Laravel: Error [PDOException]: Could not Find Driver in PostgreSQL 'Syntax Error: invalid syntax' for no apparent reason How can I fix MySQL error #1064? Notice: Trying to get property of non-object error