html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Ajax</title>
</head>
<body>
<div id="new"></div>
<script type="text/javascript" src="addLoadEvent.js"></script>
<script type="text/javascript" src="getHTTPObject.js"></script>
<script type="text/javascript" src="getNewContent.js"></script>
</body>
</html>
以下是js:
检查兼容性:
function getHTTPObject(){
if(typeof XMLHttpRequest =="undefined")
XMLHttpRequest = function (){
try {return new ActiveXObject("Msxml2.XMLHTTP.6.0");}
catch (e) {}
try {return new ActiveXObject("Msxml2.XMLHTTP.3.0");}
catch(e){}
try {return new ActiveXObject("Msxml2.XMLHTTP");}
catch(e){}
return false;
}
return new XMLHttpRequest();
}
指定发送请求:
function getNewContent(){
var request =getHTTPObject;
if(request){
request.open("GET","example.txt",true);
request.onreadystatechange = function(){
if(request.readyState ==4){
var para = document.createElement("p");
var txt = document.createTextNode(request.responseText);
para.appendChild(txt);
document.getElementById('new').appendChild(para);
}
};
request.send(null);
}else{
alert('Sorry.')
}
}
addLoadEvent(getNewContent);
用浏览器打开显示错误:
getNewContent.js:4 Uncaught TypeError: request.open is not a function
为什么??
粗心呵