Javascript Uncaught SyntaxError:Chrome 调试器中出现意外的标识符错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7732887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:07:08  来源:igfitidea点击:

Javascript Uncaught SyntaxError: Unexpected identifier error in Chrome debugger

javascriptgoogle-chrome

提问by Zeynel

I am adapting the XMLHttpRequestfrom this tutorial:

我适应XMLHttpRequest本教程

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

My code is:

我的代码是:

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

But Chrome debugger gives a Uncaught SyntaxError: Unexpected identifiererror on the else. What am I doing wrong? Thanks!

但是 Chrome 调试器Uncaught SyntaxError: Unexpected identifierelse. 我究竟做错了什么?谢谢!

回答by GNi33

You are missing the closing }before and the opening {after the else, as well as the other ones in your if-else - statement.

您错过了else}之前的结束和{之后的开始,以及 if-else - 语句中的其他内容。

It works on your tutorial code, because there's only one line in the if-else - statement. When there are multiple lines, you have to block them correctly. (I personally recommend to do this always, even if there's just one line of code. In my opinion it adds to readability and you will not have problems, when you decide to minify your code one day)

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

Try this:

试试这个:

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