Error:Uncaught SyntaxError: Unexpected token

0 votes

I am running an AJAX call in my MooTools script, this works fine in Firefox but in Chrome I am getting a

Uncaught SyntaxError: Unexpected token : error,

 I cannot determine why. Commenting out code to determine where the bad code is yields nothing, I am thinking it may be a problem with the JSON being returned. Checking in the console I see the JSON returned is this:

{"votes":47,"totalvotes":90}

I don't see any problems with it, why would this error occur?

vote.each(function(e){
  e.set('send', {
    onRequest : function(){
      spinner.show();
    },
    onComplete : function(){
      spinner.hide();
    },
    onSuccess : function(resp){
      var j = JSON.decode(resp);
      if (!j) return false;
      var restaurant = e.getParent('.restaurant');
      restaurant.getElements('.votes')[0].set('html', j.votes + " vote(s)");
      $$('#restaurants .restaurant').pop().set('html', "Total Votes: " + j.totalvotes);
      buildRestaurantGraphs();
    }
  });

  e.addEvent('submit', function(e){
    e.stop();
    this.send();
  });
});Q

How to solve this?

Oct 9 in Java-Script by kartik
• 35,820 points
163 views

1 answer to this question.

0 votes

Hello @kartik,

Using the jQuery command getJSON and adding callback=? to use JSONP and returning the JSON code {"foo":"bar"} and getting the error.

This is because I should have included the callback data, something like jQuery17209314005577471107_1335958194322({"foo":"bar"})

Here is the PHP code I used to achieve this, which degrades if JSON (without a callback) is used:

$ret['foo'] = "bar";
finish();

function finish() {
    header("content-type:application/json");
    if ($_GET['callback']) {
        print $_GET['callback']."(";
    }
    print json_encode($GLOBALS['ret']);
    if ($_GET['callback']) {
        print ")";
    }
    exit; 
}

Hope it helps!!
Thank you!!

answered Oct 9 by Niroj
• 78,120 points

Related Questions In Java-Script

0 votes
1 answer

Error: listen EADDRINUSE while using nodejs?

Hello kartik, Two possible solutions for this: Free currently ...READ MORE

answered Apr 24 in Java-Script by Niroj
• 78,120 points
338 views
0 votes
1 answer

Error: Use of const in strict mode

Hello, If this is happening in nodejs, it ...READ MORE

answered Apr 24 in Java-Script by Niroj
• 78,120 points
527 views
0 votes
1 answer

Error:“Access is denied” error on accessing iframe document object

Hello @kartik, Basically, this error occurs when the ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 78,120 points
487 views
0 votes
1 answer

Error:React-router urls don't work when refreshing or writing manually

Hello @kartik, You can change your .htaccess file and insert ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 78,120 points
3,328 views
0 votes
1 answer

jQuery AJAX fires error callback on window unload - how do I filter out unload and only catch real errors?

Hello, In the error callback or $.ajax you have three ...READ MORE

answered Apr 27 in Java-Script by Niroj
• 78,120 points
448 views
0 votes
1 answer

How do I pass command line arguments to a Node.js program?

Hello @kartik, If your script is called myScript.js ...READ MORE

answered May 5 in Java-Script by Niroj
• 78,120 points
697 views
0 votes
1 answer

Error:Issue when trying to use IN() in wordpress database

Hello @kartik, Try this code : // Create an ...READ MORE

answered May 8 in PHP by Niroj
• 78,120 points
91 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8 in Node-js by Niroj
• 78,120 points
87 views
0 votes
1 answer

Error: Unexpected token import in nodejs

Hello, Unfortunately, Node.js doesn't support ES6's import yet. To accomplish what ...READ MORE

answered Apr 24 in Java-Script by Niroj
• 78,120 points
3,252 views
0 votes
1 answer

Error:Parsing JSON giving “unexpected token o” error

Hello @kartik, Your data is already an object. ...READ MORE

answered Jun 19 in Java-Script by Niroj
• 78,120 points
373 views