Processing
 

Uncaught SyntaxError: Unexpected token < On Chrome

24/10/2013 17:57#1

Alen

Member

Joined at: 10 months ago

Post: 27

Thank: 0

Thanked: 0

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:

Uncaught SyntaxError: Unexpected token < On Chrome

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.

24/10/2013 21:13Top#2

delueg

Member

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

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>"

25/10/2013 21:26Top#3

Alen

Member

Joined at: 10 months ago

Post: 27

Thank: 0

Thanked: 0

Error with Uncaught SyntaxError: Unexpected token < using @Mario answer but that was only part of my problem. Another problem is, javascript doesn't get any data from PHP file. That was solved using this code, inside PHP file: header("Content-Type: text/javascript; charset=utf-8"); This answer is found on this link, where I opened another question to solve this issue: Can't receive json data from PHP in Chrome and Opera

17/02/2015 01:01Top#4

luckyape

Member

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

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.

25/10/2013 07:38Top#5

McRui

Member

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

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.

22/04/2016 16:16Top#6

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

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

22/07/2014 04:52Top#7

Tharanga

Member

Joined at: 7 months ago

Post: 2

Thank: 0

Thanked: 0

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!!!

05/05/2015 05:39Top#8

Joined at: 8 months ago

Post: 2

Thank: 0

Thanked: 0

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

04/11/2015 08:07Top#9

anoraq

Member

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

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.

22/04/2016 00:23Top#10

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

You may need to change how you reference your jQuery file.

Try changing type="text/javascript":

 <script type="text/javascript" src="jquery.js"></script> 

To type="text/html":

  <script type="text/html" src="jquery.js"></script> 
09/07/2016 16:11Top#11

Must Nal

Member

Joined at: 6 months ago

Post: 1

Thank: 0

Thanked: 0

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> 
Similar articles

Chrome, THREE.js: Cross-origin image load denied

5 years ago - Reply: 5 - Views: 267

Prevent Google Chrome Log XMLHttpRequest

3 years ago - Reply: 5 - Views: 276

How can I include ChromeDriver in a JAR?

3 years ago - Reply: 3 - Views: 80

Chrome geolocation not working on any site

3 years ago - Reply: 1 - Views: 43

wrong borders' width in android browser

3 years ago - Reply: 4 - Views: 205

Uncaught SyntaxError: Unexpected token < On Chrome

3 years ago - Reply: 10 - Views: 462

Flashdata not getting cleared in Codeigniter

3 years ago - Reply: 5 - Views: 57

Illegal access Javascript error in Chrome

3 years ago - Reply: 1 - Views: 51

ng-repeat not updating in Chrome

3 years ago - Reply: 1 - Views: 81

Applying border to a checkbox in Chrome

4 years ago - Reply: 2 - Views: 118

open browser plugin using access vba

3 years ago - Reply: 1 - Views: 25

vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re vé máy bay vé máy bay giá rẻ ve may bay ve may bay gia re