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

当前回答

之所以会发生这种情况,是因为我在快速服务器中设置了一个规则,将任何404路由回/#加上原始请求。允许angular router/js处理请求。如果没有js路由来处理这个路径,一个到/#/的请求就会发送到服务器,也就是一个到/的请求,整个网页。

例如,如果我想请求/correct/somejsfile.js,但我没有输入到/wrong/somejsfile.js,请求就会发送到服务器。该位置/文件不存在,因此服务器返回302位置:/#/wrong/somejsfile.js。浏览器愉快地跟随重定向,并返回整个网页。浏览器将页面解析为js,您将得到

未捕获SyntaxError:意外的令牌<

因此,为了帮助找到有问题的路径/请求,请查找302个请求。

希望这能帮助到别人。

其他回答

这个错误也可能意味着代码中缺少冒号或:。

我也遇到了同样的问题,结果是Json从服务器返回 不是有效的Json-P。如果不使用跨域调用,请使用常规Json。

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

试着忽略这个问题:

Cypress.on('uncaught:exception', (err, runnable) => {
        return false;
    });

反复面对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)"