admin管理员组文章数量:1386644
I am adapting the XMLHttpRequest
from this tutorial:
var request = new XMLHttpRequest();
request.open('GET', '/', 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", "", 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!
I am adapting the XMLHttpRequest
from this tutorial:
var request = new XMLHttpRequest();
request.open('GET', 'http://www.mozilla/', 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./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!
1 Answer
Reset to default 2You 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 remend 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:
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://ting-1.appspot./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);
本文标签: Javascript Uncaught SyntaxError Unexpected identifier error in Chrome debuggerStack Overflow
版权声明:本文标题:Javascript Uncaught SyntaxError: Unexpected identifier error in Chrome debugger - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744530717a2611018.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论