[json] Uncaught SyntaxError: Unexpected token < On Chrome

I know this question has been asked many times but I can't find similarity with my issue. I'm getting this error only in Chrome, in every other browser everything is ok. I return data with JSON in several places but since my code works in other browsers I assume nothing is wrong with it. Chrome doesn't show me where is error (in my code) it shows me these errors:

enter image description here

This is how I use JSON:

$.post("getData.php", {'id' : id}, function(data){
        var obj = jQuery.parseJSON(data);
.
.
.

... some mysqli query
$data = $query->fetch_assoc();
echo json_encode($data);

So I don't see a problem here, can someone help me with this.

This question is related to json google-chrome

The answer is


My solution to this is pretty unbelievable.

<script type="text/javascript" src="/js/dataLayer.js?v=1"></script>

The filename in the src attribute needed to be lowercase:

<script type="text/javascript" src="/js/datalayer.js?v=1"></script>

and that somewhat inexplicably fixed the problem.

In both cases the reference was returning 404 for testing.


It could be that the resource you're trying to request is under restricted access via your web app configuration, i.e. user must be logged for the application to serve the file.

Try adding this to your Web.Config file (this is for .NET applications):

<location path="js/resourcefile.js">
  <system.web>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>
</location>

You can place it anywhere before the closing configuration tag.


Had the same error. My problem was caused from shifting around my project directory, and failing to adjust the path to my static files on the server to reflect the new path (i.e. was ./src/public but should have been ./public).


header("Location: route_to_main_page");

I've encountered this problem while having a custom 404 error handling file. Instead of throwing some html content it was suppose to redirect to the main page url. It worked well.

Then I used this as a skeleton for a next project, therefore not changing the "route_to_main_page" actually on the new project (different URL) became a "route_to_external_url" so any 404 errors within your code (missing css stylesheets, js libraries) would return the "Uncaught SyntaxError: Unexpected token <".


Seems everyone has difference experiences from this and therfore solutions as well :) This is my "story".

My thing came from a validate.php file fetched with ajax. The output was meant to be :

$response['status'] = $status;
$response['message'] = $message;
$response['param'] = $param;

echo json_encode($response);

And the error that cause the "Unexpected token <" error was simply that in some cases $message hadn't been declared (but only $status and $param). So, added this in the beginning of the code.

$message = ''; // Default value, in case it doesn't get set later on.

So I guess, those "little things" may in this scenario big of quite importance. So be sure to really check your code and making it bulletproof.


I have commented my this code : // $('#description').val('<?php echo $_POST['description']; ?>'); and I got that error.


If you are trying to perform Ajax file upload or something similar, you have to get rid of displaying further HTML after printing your ajax response text. Probably that gives this error message. If you are using PHP framework you have to follow framework syntax to end the application. If you are coding pure PHP, you can use 'exit' after your ajax response text.

Hope this will help save some ones' time on finding solution for this. :) happy coding!!!


To override the error that you might experience in Chrome (and probably in Safari), try to set the Ajax parameter as dataType: "json". Then you shouldn't call parseJSON() on the obj because the response you'll get comes deserialized.


The only place it worked for me is when I place the scripts in public folder where my index.html resides and then placing these <script type="text/javascript" src="test/test.js"></script> inside <body> tag.


You can check your Network (console) and See the answer from the Server ... The "<" will be the first letter of your response. Something like "<"undefined index XY in line Z>"


change it to

$.post({ 
         url = 'getData.php',  
         data : { 'id' id } , 
         dataType : 'text'
       });

This way ajax will not try to parse the data into json or similar


I faced similar issue when I moved some of the js files into folders then deployed into production, after some struggle I found out that the js files were not actually got deployed to production. So

  1. make sure that the file for which the error is shown does exist in the server
  2. the path given is correct

So, In the absence of js file, the server then server responds with as below

<!doctype html>
    <html>
        <someTag />
        <AnotherTag />
    </html>

Thats why we get Uncaught SyntaxError: Unexpected token <


I got the same error ("Uncaught SyntaxError: Unexpected token <" ) at these two lines when testing a sample application .

<script type = "text/javascript"  src="raphael-min.js"></script>
<script type = "text/javascript"  src="kuma-gauge.jquery.js"></script>

After a control, I realized that, local file locations are not correct and my local server app returns default page as the result. Client app can find the files but the founded files are default page, not the *.js files. So I receive Uncaught SyntaxError: Unexpected token <

I changed to orginal location on the intenet andit solved.

<script type = "text/javascript"  src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.2/raphael-min.js"></script>
<script type = "text/javascript"  src="//www.jqueryscript.net/demo/Creating-Animated-Gauges-Using-jQuery-Raphael-js-kumaGauge/js/kuma-gauge.jquery.js"></script>

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

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?