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

当前回答

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

其他回答

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

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

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

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

虽然迟到了,但我的解决方案是将dataType指定为json。或者确保您没有设置jsonp: true。

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