Saturday, 14 September 2013

Cleaner Syntax for HTTP Object in AJAX?

Cleaner Syntax for HTTP Object in AJAX?

AJAX noob here... So this code below works for me which is called during
an onclick event:
function updateText() {
var http = getHTTPObject();
http.open("GET","ajax_info.asp",true);
http.onreadystatechange = function() {
if (http.readyState == 4) {
document.getElementById("myDiv").innerHTML=http.responseText;
}
}
http.send();
}
However, when I tried to clean it like below, it no longer works. Is the
code below not a correct callback syntax?
function handleHTTPResponse() {
if (http.readyState == 4) {
document.getElementById("myDiv").innerHTML=http.responseText;
}
}
function updateText() {
var http = getHTTPObject();
http.open("GET","ajax_info.asp",true);
http.onreadystatechange = handleHTTPResponse;
http.send();
}

No comments:

Post a Comment