我在我的MooTools脚本中运行一个AJAX调用,这在Firefox中工作得很好,但在Chrome中,我得到了一个未捕获的SyntaxError:意外令牌:错误,我不能确定为什么。注释掉代码来确定坏代码在哪里不会产生任何结果,我认为这可能是JSON返回的问题。检查控制台,我看到返回的JSON是这样的:

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

我没有看到它有任何问题,为什么会出现这个错误?

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

当前回答

我刚刚解决了这个问题。有一些东西导致了标准请求调用的问题,所以这是我使用的代码:

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

如果有人知道为什么标准请求对象给我的问题,我很想知道。

其他回答

看到红色错误

未捕获SyntaxError:意外的令牌<

在Chrome开发人员的控制台选项卡中是响应体中的HTML指示。

您实际看到的是浏览器对意想不到的顶部行<!DOCTYPE html>从服务器。

当你的数据返回错误的json格式时出现“Uncaught SyntaxError: Unexpected token”错误,在某些情况下,你不知道你得到了错误的json格式。 请用alert()检查;函数

onSuccess : function(resp){  
   alert(resp);  
}

你收到的信息应该是:{"firstName":"John", "lastName":"Doe"} 然后您可以使用下面的代码

onSuccess : function(resp){  
   var j = JSON.decode(resp); // but in my case i'm using: JSON.parse(resp); 
}

“未捕获SyntaxError:意外令牌” 但如果json格式有误 例:

...{“firstName”:“约翰”,“姓”:“母鹿”}

or

Undefined variable: errCapt in .... on line<b>65</b><br/>{"firstName":"John", "lastName":"Doe"}

所以你得到了错误的json格式,请在json .decode或json .parse之前修复它

我做错了

   `var  fs = require('fs');
    var fs.writeFileSync(file, configJSON);`

我已经初始化了fs变量。但我还是把var放在了第二行。这个也给出了那种错误。

我刚刚解决了这个问题。有一些东西导致了标准请求调用的问题,所以这是我使用的代码:

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

如果有人知道为什么标准请求对象给我的问题,我很想知道。

只是供那些可能有同样问题的人参考——我只需要让我的服务器以application/ JSON的形式发送回JSON,默认的jQuery处理程序就可以正常工作。