Javascript Uncaught SyntaxError:Chrome 调试器中出现意外的标识符错误
我正在改编本教程中的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);
我的代码是:
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:意外标识符
错误。我做错了什么?谢谢!
I am adapting the XMLHttpRequest
from this tutorial:
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 identifier
error on the else
. What am I doing wrong? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少 else 之前的结束
}
和 else 之后的开始{
,以及 if-else - 语句中的其他内容。它适用于您的教程代码,因为 if-else - 语句中只有一行。当有多条线时,你必须正确地阻挡它们。 (我个人建议始终这样做,即使只有一行代码。在我看来,它会增加可读性,并且当您决定有一天缩小代码时,您不会遇到问题)
尝试以下操作:
You are missing the closing
}
before and the opening{
after the else, as well as the other ones in your if-else - statement.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)
Try this: