Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 902
Alen Avatar asked Oct 24 '13 17:10

Alen


People also ask

What does it mean by uncaught SyntaxError unexpected token?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

How do you solve uncaught SyntaxError unexpected token export?

To solve the "Uncaught SyntaxError Unexpected token 'export'" error, set the type of your <script /> tags to module , e.g. <script type="module" src="index. js"></script> . The type="module" attribute is supported in all modern browsers and allows us to use the ES6 modules syntax. Copied!

How do I fix uncaught SyntaxError unexpected identifier?

To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.


4 Answers

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

like image 122
Sven Delueg Avatar answered Nov 27 '22 11:11

Sven Delueg


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.

like image 27
luckyape Avatar answered Nov 27 '22 13:11

luckyape


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

like image 29
Alen Avatar answered Nov 27 '22 12:11

Alen


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>
like image 44
Must Nal Avatar answered Nov 27 '22 13:11

Must Nal