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

当前回答

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

其他回答

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

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

看到红色错误

未捕获SyntaxError:意外的令牌<

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

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

未捕获的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'" ...

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

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

当你的数据返回错误的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之前修复它