您的位置:首页 > 编程语言 > PHP开发

Ajax and php 2_6

2016-03-20 12:13 555 查看
</pre><pre name="code" class="javascript"><p>
</p><p><DOCTYPE html></p><html>
<head>
<title>2_6</title>
<meta charset="utf-8"/>
</head>
<body onload="process()">
<div id="myDiv">

</div>
</body>
<script>
var xmlHttp=createXmlHttpRequestObject();
function createXmlHttpRequestObject(){
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest();
}catch(e){
var XmlHttpVersions=new Array(
"MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Mircosoft.XMLHTTP"
);

for(var i=0;i<XmlHttpVersions.length&&!xmlHttp;i++){
try{
xmlHttp=new ActiveXObject(XmlHttpVersion[i]);
}catch(e){
}
}
}
if(xmlHttp){
return xmlHttp;
}else{
alert('Error create the XMLHttpRequest object.');
}
}

function process(){
if(xmlHttp){
try{
xmlHttp.open("GET","books.xml",true);
xmlHttp.onreadystatechange=handleRequestStateChange;
xmlHttp.send(null);
}catch(e){
alert("Can't connect to server:\n"+e.toString());
}
}
}

function handleRequestStateChange(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
try{
handleServerResponse();
}catch(e){
alert("Error reading the response:"+e.toString());
}
}else{
alert("There was a problem retrieving the data:\n"+xmlHttp.statusText);
}
}
}

function handleServerResponse(){
var xmlResponse=xmlHttp.responseXML;
//XML文件格式一定要正确
xmlRoot=xmlResponse.documentElement;
//注意getElementsByTagName
titleArray=xmlRoot.getElementsByTagName("title");
isbnArray=xmlRoot.getElementsByTagName("isbn");

var html="";
for(var i=0;i<titleArray.length;i++){
html+=titleArray.item(i).firstChild.data+","+isbnArray.item(i).firstChild.data+"<br/>";
}
myDiv=document.getElementById('myDiv');
myDiv.innerHTML="Server says:<br/>"+html;
}
</script>
</html>


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: