Monday, May 20, 2024
123
rated 0 times [  127] [ 4]  / answers: 1 / hits: 16782  / 13 Years ago, tue, october 11, 2011, 12:00:00

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!


More From » google-chrome

 Answers
35

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:



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

[#89671] Monday, October 10, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maximusbradforde

Total Points: 594
Total Questions: 106
Total Answers: 82

Location: Tuvalu
Member since Sat, Feb 11, 2023
1 Year ago
;