首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Javascript未捕获SyntaxError: Chrome调试器中出现意外的标识符错误

Javascript未捕获SyntaxError: Chrome调试器中出现意外的标识符错误
EN

Stack Overflow用户
提问于 2011-10-12 05:42:38
回答 1查看 16.3K关注 0票数 1

我正在改编来自this tutorialXMLHttpRequest

代码语言:javascript
复制
var request = new XMLHttpRequest();  
request.open('GET', 'http://www.mozilla.org/', true);  
request.onreadystatechange = function (aEvt) {  
  if (request.readyState == 4) {  
     if (request.status == 200)  
       console.log(request.responseText)  
     else  
       console.log('Error', request.statusText);  
  }  
};  
request.send(null);

我的代码是:

代码语言:javascript
复制
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/submithandlertest", true);
xhr.onreadystatechange = function (aEvt) {
  if (xhr.readyState == 4) {
      if (xhr.status == 200) 
          console.log("request 200-OK");
          chrome.browserAction.setBadgeText ( { text: "done" } );
      else
          console.log("connection error");
          chrome.browserAction.setBadgeText ( { text: "ERR" } );
      setTimeout(function () {
      chrome.browserAction.setBadgeText( { text: "" } );
      }, 2000);
  }        
}        
xhr.send(formData);

但是Chrome调试器在else上显示Uncaught SyntaxError: Unexpected identifier错误。我做错了什么?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-12 05:44:57

在if-else -语句中,缺少前面的结束}和后面的开始{,以及其他语句。

它适用于您的教程代码,因为if-else -语句中只有一行。当有多行时,您必须正确地阻止它们。(我个人建议始终这样做,即使只有一行代码。在我看来,它增加了可读性,当你决定某一天缩减你的代码时,你不会有问题。)

试试这个:

代码语言:javascript
复制
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot.com/submithandlertest", true);
xhr.onreadystatechange = function (aEvt) {
  if (xhr.readyState == 4) {
      if (xhr.status == 200){
          console.log("request 200-OK");
          chrome.browserAction.setBadgeText ( { text: "done" } );
      }else{
          console.log("connection error");
          chrome.browserAction.setBadgeText ( { text: "ERR" } );
      setTimeout(function () {
      chrome.browserAction.setBadgeText( { text: "" } );
      }, 2000);
    }
  }        
};    
xhr.send(formData);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7732887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档