L
lawpoop
I'm trying to write a generic ajax get function. I want to return the
response from the get request, so that I can reuse it in other
scripts. Perhaps I want to use it in an innerHtml, perhaps I want to
put it in an alert().
I have this, which works:
function ajaxGetResponse( page ) {
var xmlHttp;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp=new ActiveXObject
("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support
AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function() {
if ( xmlHttp.readyState == 4 ) {
alert ( xmlHttp.responseText );
}
}
xmlHttp.open( "GET", page, true );
xmlHttp.send(null);
}
In other words, I get an alert of the value of the page that I'm
requesting.
However, I tried simply changing alert() to return, and in IE, I get
an alert box which says simply "undefined".
function ajaxGetResponse( page ) {
var xmlHttp;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp=new ActiveXObject
("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support
AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function() {
if ( xmlHttp.readyState == 4 ) {
return xmlHttp.responseText ;
}
}
xmlHttp.open( "GET", page, true );
xmlHttp.send(null);
}
What am I doing wrong?
response from the get request, so that I can reuse it in other
scripts. Perhaps I want to use it in an innerHtml, perhaps I want to
put it in an alert().
I have this, which works:
function ajaxGetResponse( page ) {
var xmlHttp;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp=new ActiveXObject
("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support
AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function() {
if ( xmlHttp.readyState == 4 ) {
alert ( xmlHttp.responseText );
}
}
xmlHttp.open( "GET", page, true );
xmlHttp.send(null);
}
In other words, I get an alert of the value of the page that I'm
requesting.
However, I tried simply changing alert() to return, and in IE, I get
an alert box which says simply "undefined".
function ajaxGetResponse( page ) {
var xmlHttp;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp=new ActiveXObject
("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support
AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function() {
if ( xmlHttp.readyState == 4 ) {
return xmlHttp.responseText ;
}
}
xmlHttp.open( "GET", page, true );
xmlHttp.send(null);
}
What am I doing wrong?