So, I lifted this nice AJAX code from another site. Problem is that I such a lame JavaScript programmer that I can figure out how to get the output onto my page. My startUp() function says o is undefined. I do see the results of the php on the Firebug console.
TIA for your help.
var asyncRequest = function() {
function handleReadyState(o, callback) {
if (o && o.readyState == 4 && o.status == 200) {
if (callback) {
callback(o);
}
}
}
var getXHR = function() {
var http;
try {
http = new XMLHttpRequest;
getXHR = function() {
return new XMLHttpRequest;
};
}
catch(e) {
var msxml = [
'MSXML2.XMLHTTP.3.0',
'MSXML2.XMLHTTP',
'Microsoft.XMLHTTP'
];
for (var i=0, len = msxml.length; i < len; ++i) {
try {
http = new ActiveXObject(msxml[i]);
getXHR = function() {
return new ActiveXObject(msxml[i]);
};
break;
}
catch(e) {}
}
}
return http;
};
return function(method, uri, callback, postData) {
var http = getXHR();
http.open(method, uri, true);
handleReadyState(http, callback);
http.send(postData || null);
return http;
};
}();
asyncRequest('GET', 'voterserver.php', function(o) {
console.log(o.responseText);
});
function startUp() {
//alert("Here!");
document.getElementById("user_list").innerHTML=o.responseText;
}
The asyncRequest takes a callback function as the 3rd parameter thats the
within it
the value of o passed to that function and is not available in your startUp function.
you need to rewrite your startup as follows
I’ve set the indentation to make it clearer.
You may want too look at using one of the Javascript frameworks that makes this sort of thing alot easier and has good documentation, I use mootools but JQuery is very popular.
UPDATE
I tried to make a JSFIddle with thte code you put in your question and it didnt work, I’ve made changes to it to get it to work and its Here for you to try.
Same thing with MooTools MooTools