189

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?

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
dlaurent86
  • 2,115
  • 3
  • 14
  • 12
  • 3
    Checking the response in a network sniffer might give you a clue. My guess is that the `Content-length` header specifies more bytes than the response contains, or maybe the server somehow sends invalid HTML. – tdammers Aug 29 '10 at 13:33
  • 4
    Missing } most the time (javascript). Check the end of your classes and functions. Try just adding another closing } at the end of your script and re-autoformat your code. If there is any strange indentation in your code, then somewhere right before that is most likely the spot where a } has gone missing. – OG Sean Jul 26 '18 at 00:52
  • I had this issue when loading JS incorrectly. I loaded it as `` and it should have been `` – Daniel Apt Sep 11 '18 at 09:10
  • I am getting this error today on Google Sheets. My guess is that one of their JS files is so big that its getting terminated without loading fully. Or one of their app servers has a bug and is closing the http connection before the JS file is fully downloaded. – Teddy Apr 29 '20 at 14:09

20 Answers20

237

This particular error is one annoying fact about . In most cases your JavaScript is broken in some way. For example missing a } or something like that.

Example given, this will yield "Unexpected end of input" too:

eval('[{"test": 4}') // notice the missing ]

But the root cause of the problems seems to be that the requested JSON url has a Content-Type of text/html which Chrome apparently tries to parse as HTML, which then results in the unexpected end of input due to the fact that the included image tags are being parsed.

Try setting the Content-Type to text/plain I think it should fix the issues.

Nonetheless, V8 could do a better Job about telling one exactly where the input ended unexpectedly.

Nae
  • 14,209
  • 7
  • 52
  • 79
Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
  • 3
    ok Thanks alot i removed the json request all together and the error went away i've yet to figure out what exactly is wrong with my json request. i just had no idea where to start debugging. if i may ask how did you determine the root cause – dlaurent86 Aug 29 '10 at 13:52
  • 2
    It might not be that obvious. I've gotten this error on minified JS due to it being mangled by the minimizer, whereas the unminified JS was perfectly legal and valid. Unfortunately, Chrome only shows the line where the error occurs, which for minimized JS is usually the entire file... – Cerin Jan 08 '14 at 17:17
  • Not so much where the input ended (cuz that would be at the end). Rather, knowing *what* was expected would help tons. – AndrewS Jun 09 '14 at 22:07
  • Saved my tail. I was getting terribly annoyed by the ambiguity. Thanks. – Cyprus106 Aug 19 '14 at 20:17
  • 4
    I had the same error, because I was using JSON.parse(text) and text value was empty string – A Khudairy Feb 24 '15 at 16:50
  • I am getting this error in chrome only and firefox is clean and only in few chrome client have this issue. I wish to change the content-type as you suggest but I am not able to get where is the required change needs to be done for text/plain - I am using aws s3 and cloudfront for serving the pages - and this error source is in angularjs files loaded from cdn and not from a custom endpoint on my server - please guide – Abdeali Chandanwala Apr 23 '19 at 07:16
  • Content-Type text/plain is letting the webView display all codes as plain text, you should use text/HTML or text/javascript instead also text/css – Bay Sep 17 '19 at 10:41
78

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

http://getfirebug.com/

34

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("");
Community
  • 1
  • 1
falsarella
  • 12,217
  • 9
  • 69
  • 115
10

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.

DroidOS
  • 8,530
  • 16
  • 99
  • 171
  • Never would have figured this out without you. I was using VideoJS and I copied the html for their video player from their page. It had a data-attribute placeholder you could use but I wasn't. The video wasn't loading and I was getting the error everyone is talking about. After deleting `data-setup='{"example_option":true}' ` Everything worked great. – Tyler Buchea Mar 08 '15 at 22:48
10

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

Hubro
  • 56,214
  • 69
  • 228
  • 381
HBP
  • 15,685
  • 6
  • 28
  • 34
8

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.

Blaskovicz
  • 6,122
  • 7
  • 41
  • 50
7

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

Harfatum
  • 211
  • 2
  • 5
6

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!

Andrew
  • 227,796
  • 193
  • 515
  • 708
3

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

Hawken MacKay Rives
  • 1,171
  • 1
  • 16
  • 26
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
3

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.

Amir
  • 1,031
  • 2
  • 19
  • 42
1

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'); }
Johan Doe
  • 11
  • 2
  • Worked, Plus, you should use \" the-text \" as refers to "the-text" also for casting. – Bay Sep 17 '19 at 10:49
1

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.

George_kane
  • 305
  • 3
  • 4
  • 17
  • using addthis I appended to the url with double string and then changed to single quote and the console error disappeared – tfa Apr 23 '20 at 16:57
1

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); 
jtate
  • 2,612
  • 7
  • 25
  • 35
rivelbab
  • 131
  • 4
1

One of the reasons for this error can be network infrastructure. I've got this problem by using a web proxy service from Cloudflare. As soon as I disable proxy and cleared browser cache it started working. So check the status of your network components too. In my case it was communicated at Cloudflare status page enter image description here

Sergey V.
  • 840
  • 8
  • 15
0

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.

OlehZiniak
  • 933
  • 1
  • 13
  • 29
0

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

safhac
  • 1,141
  • 8
  • 14
0

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
   }
};
Brick
  • 3,998
  • 8
  • 27
  • 47
0

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

0

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

0

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

Shahid Islam
  • 559
  • 3
  • 7