我在我的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'" ...

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

其他回答

我得到这个错误,因为我在脚本标签中缺少类型属性。

最初我使用,但当我在脚本标记内添加类型属性时,我的问题就解决了

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

错误的代码是:

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

和正确的代码:

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

如果没有任何意义,这个错误也可能是由嵌入在html/javascript中的PHP错误引起的,比如下面这个

<br />
<b>Deprecated</b>:  mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in <b>C:\Projects\rwp\demo\en\super\ge.php</b> on line <b>54</b><br />
var zNodes =[{ id:1, pId:0, name:"ACE", url: "/ace1.php", target:"_self", open:true}

不是PHP插入到html中的代码中的<br />等导致了错误。要修复这类错误(抑制警告),请在开始时使用此代码

error_reporting(E_ERROR | E_PARSE);

要查看,右键单击页面,“查看源代码”,然后检查完整的html以发现此错误。

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

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

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

我做错了

   `var  fs = require('fs');
    var fs.writeFileSync(file, configJSON);`

我已经初始化了fs变量。但我还是把var放在了第二行。这个也给出了那种错误。