ajax 技术
发布时间:2020-12-15 21:42:44 所属栏目:百科 来源:网络整理
导读:php编程资源下载 ajax 开发简略 ajax: (Asynchronous javascript and xml ) ajax 是多种技术的综合: xml,css,javascript,xstl,dom,xhtml,XMLHttpRequest ajax能解决网页上出现的多种问题: 1.页面无刷新的动态数据交换 2.局部刷新页面 3.界面的美观 4.对数
php编程资源下载 ajax 开发简略 ajax:(Asynchronous javascript and xml ) ajax 是多种技术的综合:xml,css,javascript,xstl,dom,xhtml,XMLHttpRequest ajax能解决网页上出现的多种问题: 1.页面无刷新的动态数据交换 2.局部刷新页面 3.界面的美观 4.对数据库的操作 5.可以返回简单的文本格式,也可以返回xml格式和json格式 ajax 是一个与服务器端语言无关的技术,可以用在php,asp/.net,javaee等网站上。 无刷新的数据交换技术有:flash,java applet,iframe,框架,ajax
===========================我只是一条分隔线============================== ajax 案例 1.局部提交数据 register.php <html> <head> <TITLE>用户注册</TITLE> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <script type="text/javascript"> //创建ajax引擎 function getXmlHttpObject(){ //不同的浏览器获取对象xmlHttpRequest方法不一样 var xmlHttp=null; try { // Firefox,Opera 8.0+,Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer window.alert("ie"); try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } var myXmlHttpRequest=null; //验证用户名是否存在 function checkName(){ myXmlHttpRequest = getXmlHttpObject(); if(myXmlHttpRequest != null){ //通过myXmlHttpRequest对象发送请求到服务器的某个页面 /* @param1:表示请求的方式,get/post @param2:指定URL,表示对那个页面发出ajax请求(http请求) @param3:表示使用异步机制 */ //get请求 //使用get方式发出请求,如果提交的用户名不变化,浏览器将不会真的发出请求,而是缓存数据 //解决方案: //1.url后带一个总是变化的参数,比如当前时间 //2.在服务器回送结果时,禁用缓存 // var url = "./registerProcess.php?mytime="+new Date()+"&username="+$("username").value; //打开请求 // myXmlHttpRequest.open("get",url,true); // //指定回调函数 // myXmlHttpRequest.onreadystatechange=process; // //发送数据 // myXmlHttpRequest.send(null); //post方式请求 var url="./registerProcess.php"; var data="username="+$('username').value; myXmlHttpRequest.open("post",true); myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //指定回调函数 myXmlHttpRequest.onreadystatechange=process; //发送数据 myXmlHttpRequest.send(data); } } function process(){ //去除registerProcess.php返回的数据 if(myXmlHttpRequest.readyState==4){ //当返回的数据是纯文本时 //$('myres').value = myXmlHttpRequest.responseText; //当返回的数据是xml格式时 //取出message结点列表数据 //var messageList = myXmlHttpRequest.responseXML.getElementsByTagName("message"); // //取出message数据 // var msg = messageList[0].childNodes[0].nodeValue; // $('myres').value = msg; //当返回的数据是json时 var res =myXmlHttpRequest.responseText; //使用eval函数解析字符串 var res_obj = eval("("+res+")"); $('myres').value = res_obj.res; } } // function $(id){ return document.getElementById(id); } </script> </head> <body> <FORM action="" method="post"> 用户名:<INPUT type="text" onkeyup="checkName();" name="username" id="username"> <INPUT type="button" onclick="checkName();" value="验证用户名"> <INPUT style="border-width:0;color:red" type="text" id="myres"> <br/> 用户密码:<INPUT type="password" name="password"><br/> 电子邮件:<INPUT type="text" name="email"><br/> <input type="submit" value="用户注册"> </FORM> <FORM action="" method="post"> 用户名字:<INPUT type="text" name="username2"> <br/> 用户密码:<INPUT type="password" name="password"><br/> 电子邮件:<input type="text" name="email"><br/> <INPUT type="submit" value="用户注册"> </FORM> </body> </html> <?php ?>
registerProcess.php <?php //接收数据,告诉浏览器返回的数据时xml格式 header("Content-type: text/xml; charset=utf-8"); //告诉浏览器不要缓存数据 header("Cache-Control:no-cache"); //当使用get请求时 //$username=$_GET['username']; //当使用post 请求 $username=$_POST['username']; $info =""; if($username=="zhengjinwei"){ //当返回文本时 //$info = "此用户名不可使用"; //当以xml格式返回时 // $info .= "<resource> // <message>此用户名不可使用</message> // </resource>"; //当以json格式返回数据时 $info .= '{"res":"此用户名不可使用","id":"001"}'; }else{ //当返回文本时 //$info = "此用户名可以使用"; //当以xml格式返回时 // $info .= "<resource> // <message>此用户名可以使用</message> // </resource>"; //当以json格式返回数据时 $info .= '{"res":"此用户名可以使用","id":"002"}'; } echo $info; ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |