<html>
<title>AJAX return text</title>
<head>
<script type = "text/javascript">
function getXMLHTTPRequest(){
var req = false;
try {
req = new XMLHttpRequest();
}catch(err1){
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
}catch(err2){
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(err3){
req = false;
}
}
}
return req;
}
var http = getXMLHTTPRequest();
function getServerText(){
var myurl = "http://www.weather.com/weather/today/Hong+Kong+China+CHXX0049";
http.open("GET", myurl, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}
function useHttpResponse(){
if( http.readyState == 4){
var mytext = http.responseText;
alert(mytext);
}else{
document.getElementById('test').innerHTML = 'Not fetched';
}
}
</script>
</head>
<body onload="getServerText()">
<div id='test'></div>
</body>
</html>>
thx meffe!
This is what I have, but the alert box just give me some blank msg.
It gives me "null" if I replace http.responseText with http.responseXML
What's wrong with my coding?