Chrome: Uncaught SyntaxError: Unexpected end of input

519,404

Solution 1

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.

Solution 2

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("");

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
519,404

Related videos on Youtube

dlaurent86
Author by

dlaurent86

Updated on September 15, 2021

Comments

  • dlaurent86
    dlaurent86 over 2 years

    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?

    • tdammers
      tdammers over 13 years
      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.
    • OG Sean
      OG Sean over 5 years
      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.
  • dlaurent86
    dlaurent86 over 13 years
    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
  • Cerin
    Cerin over 10 years
    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...
  • AndrewS
    AndrewS almost 10 years
    Not so much where the input ended (cuz that would be at the end). Rather, knowing what was expected would help tons.
  • Cyprus106
    Cyprus106 over 9 years
    Saved my tail. I was getting terribly annoyed by the ambiguity. Thanks.
  • A Khudairy
    A Khudairy about 9 years
    I had the same error, because I was using JSON.parse(text) and text value was empty string
  • Tyler Buchea
    Tyler Buchea about 9 years
    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.
  • DroidOS
    DroidOS about 9 years
    @user1002379, happy I could help.
  • Dustin Poissant
    Dustin Poissant about 9 years
    I started a ajax request (upload) and aborted it before it completed and I got this error.
  • cronoklee
    cronoklee almost 9 years
    Yes I had this one also! You can solve easily by providing "{}" instead of an empty string
  • jpierson
    jpierson over 8 years
    I was receiving empty json due to a double forward slash in a URL (ex. localhost:8080//mypath/blagh) and the server was returning an empty response for this.
  • Luís Assunção
    Luís Assunção about 8 years
    I came here just to confirm this solution.
  • Abdeali Chandanwala
    Abdeali Chandanwala almost 5 years
    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
  • Bay
    Bay over 4 years
    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
    Bay over 4 years
    Worked, Plus, you should use \" the-text \" as refers to "the-text" also for casting.
  • tfa
    tfa almost 4 years
    using addthis I appended to the url with double string and then changed to single quote and the console error disappeared

Related