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

当前回答

未捕获的SyntaxError:意外的令牌}

Chrome给了我这个示例代码的错误:

<div class="file-square" onclick="window.location = " ?dir=zzz">
    <div class="square-icon"></div>
    <div class="square-text">zzz</div>
</div>

并解决了它,固定的onclick就像

... onclick="window.location = '?dir=zzz'" ...

但是这个错误和这个问题无关。

其他回答

今天我也遇到了这种情况。我使用EF和返回一个实体响应AJAX调用。实体上的虚拟属性导致服务器上未检测到的周期性依赖项错误。通过在虚拟属性上添加[ScriptIgnore]属性,这个问题得到了解决。

与其使用ScriptIgnore属性,不如直接返回DTO。

反复面对JS问题,我正在我的xblock包上应用Ckeditor。如果有人帮助我,请告诉我。使用OpenEdx, Javascript, xblock

xblock.js:158 SyntaxError: Unexpected token '=>'
at eval (<anonymous>)
at Function.globalEval (jquery.js:343)
at domManip (jquery.js:5291)
at jQuery.fn.init.append (jquery.js:5431)
at child.loadResource (xblock.js:236)
at applyResource (xblock.js:199)
at Object.<anonymous> (xblock.js:202)
at fire (jquery.js:3187)
at Object.add [as done] (jquery.js:3246)
at applyResource (xblock.js:201) "SyntaxError: Unexpected token '=>'\n    at eval (<anonymous>)\n    at Function.globalEval (http://localhost:18010/static/studio/common/js/vendor/jquery.js:343:5)\n    at domManip (http://localhost:18010/static/studio/common/js/vendor/jquery.js:5291:15)\n    at jQuery.fn.init.append (http://localhost:18010/static/studio/common/js/vendor/jquery.js:5431:10)\n    at child.loadResource (http://localhost:18010/static/studio/bundles/commons.js:5091:27)\n    at applyResource (http://localhost:18010/static/studio/bundles/commons.js:5054:36)\n    at Object.<anonymous> (http://localhost:18010/static/studio/bundles/commons.js:5057:25)\n    at fire (http://localhost:18010/static/studio/common/js/vendor/jquery.js:3187:31)\n    at Object.add [as done] (http://localhost:18010/static/studio/common/js/vendor/jquery.js:3246:7)\n    at applyResource (http://localhost:18010/static/studio/bundles/commons.js:5056:29)"

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

错误的代码是:

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

和正确的代码:

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

对我来说,当我在Chrome浏览器中查看页面的源代码时,灯泡就亮了。我在if语句中多了一个括号。您将立即在失败的行上看到一个带叉的红圈。这是一个相当没有帮助的错误消息,因为“未捕获语法错误:意外令牌”第一次出现在Chrome控制台时没有引用行号。

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

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

我把上面的映射改为

 @RequestMapping("/private/updatestatus")

or

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