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

当前回答

我的错误是在javascript中忘记了url周围的单/双引号:

错误的代码是:

window.location = https://google.com;

和正确的代码:

window.location = "https://google.com";

其他回答

当我使用jQuery.getJSON()试图反序列化无穷大的浮点值时,我得到了一个“SyntaxError:意外令牌I”,编码为INF,这在JSON中是非法的。

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

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

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

在我的情况下,我遇到了同样的错误,而运行spring mvc应用程序由于错误的映射在我的mvc控制器

@RequestMapping(name="/private/updatestatus")

我把上面的映射改为

 @RequestMapping("/private/updatestatus")

or

 @RequestMapping(value="/private/updatestatus",method = RequestMethod.GET)

在我的例子中,把/放在脚本的src或样式表的href的开头就解决了这个问题。

这种情况刚刚发生在我身上,而原因并不是上述原因中的任何一个。我使用jQuery命令getJSON和添加回调=?使用JSONP(因为我需要跨域),并返回JSON代码{"foo":"bar"}并得到错误。

这是因为我应该包括回调数据,比如jQuery17209314005577471107_1335958194322({"foo":"bar"})

下面是我用来实现这一点的PHP代码,如果使用JSON(没有回调),则会降级:

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

希望这能在将来帮助到别人。