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>
相关文章推荐
- PHP算法分析
- php7如何加入环境变量
- 新手学习PHP的经典算法(1):分苹果
- jmeter测试FTP服务器性能和FTP服务器搭建
- yii2遇到的问题 自己找
- php fsockopen curl file_get_contents
- 安装php出现“/replace/with/path/to/perl/interpreter: bad interpreter: No such file or directory ”的解决方法
- PHP数据类型转换和运算符表达式
- PHP数组
- PHP基础
- php函数
- ThinkPHP 常用配置 和 四种url访问方式
- PHP学习笔记-PHP概述与环境搭建
- php 的设计模式
- PHP empty、isset、isnull的区别
- PHP函数
- 1、php基本语法--函数
- PHP中变量的声明、作用范围和解析规则
- 分享一个js文件中嵌套php会出错的问题
- PHP二维数组提取函数----把不需要的数据剔除