Search code examples
javascriptmootoolsgoogle-chrome

Uncaught SyntaxError: Unexpected token :


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

Solution

  • I have just solved the problem. There was something causing problems with a standard Request call, so this is the code I used instead:

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

    If anyone knows why the standard Request object was giving me problems I would love to know.