客户端POST提交、服务器返回Xml格式数据的demo
2014-10-05 09:54
447 查看
//register.php <html> <head> <title>ajax用户注册</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style type="text/css"> body{ font-family: "Microsoft JhengHei"; } label{ font-weight: bolder; font-size: 14px; } input.check{ border-width: 0; color: red; font-family: "Microsoft JhengHei"; font-weight: bolder; } </style> <script type="text/javascript"> function getXmlHttpObject(){ var xmHttpRequset; if(window.ActiveXObject){ //window.alert("ie") xmlHttpRequset = new ActiveXObject("Microsoft.XMLHTTP"); }else{ //window.alert("ff") xmlHttpRequset = new XMLHttpRequest(); } return xmlHttpRequset; } var myXmlHttpRequset = getXmlHttpObject(); //申明myXmlHttpRequset为全局变量 function checkName(){ if(myXmlHttpRequset){ var myurl = "/ajax/learn/registerpro.php"; var data = "username=" + $("username").value; myXmlHttpRequset.open("post", myurl, true); myXmlHttpRequset.onreadystatechange = solve; myXmlHttpRequset.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); myXmlHttpRequset.send(data); }else{ //window.alert("error"); } } function $(id){ return document.getElementById(id); } //回调函数 function solve(){ if(myXmlHttpRequset.readyState == 4){ var mes = myXmlHttpRequset.responseXML.getElementsByTagName("mes"); var mes_val = mes[0].childNodes[0].nodeValue; $('res').value = mes_val; } } </script> </head> <body> <form action="registerpro.php" method="post"> <label>账号</label> <input type="text" name="username" id="username" /> <input type="button" name="check" id="check" value="检查用户名" onclick="checkName()"/> <input class="check" type="text" id="res" readonly="readonly"/> </br> <label>密码</label> <input type="text" name="password" id="password" /> <input type="submit" name="submit" id="submit"/> </form> </body> </html>
//registerpro.php //告诉浏览器,是xml文件,同时不要缓存数据 header("Content-Type: text/xml; charset=utf-8"); header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); $username = $_POST['username']; $info = ""; if($username == "lisheng"){ $info .= "<res><mes>用户名不可用</mes></res>" }else{ $info .= "<res><mes>用户名可用</mes></res>" } echo $info;
相关文章推荐
- 客户端POST提交、服务器返回json格式数据的demo
- 客户端GET方法提交数据,服务器返回文件格式数据的demo
- Android之使用HttpPost提交数据到服务器(Android手机客户端和后台服务器交互)
- php webservice实现客户端提交数据库数据到服务器并返回另一份数据库数据
- C# 模拟post数据提交时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
- java,用post方法提交数据,并且从服务器返回结果。
- 获取客户端ip地址方法和通过curl在命令行提交post数据到服务器
- 用node.js搭建服务器,模拟返回json数据供客户端get,post请求使用
- 数据提交到服务器并且返回数据给客户端的过程
- HttpUrlConnection Post提交数据到服务器、并得到服务器返回的数据
- 客户端提交查询请求成功后如何向服务器返回数据
- HttpUrlConnection Post提交数据到服务器、并得到服务器返回的数据
- AJAX通过post提交数据,并获取XML格式返回结果
- http协议从客户端提交数据给服务器并返回数据
- 服务器获得客户端提交的数据及中文乱码问题
- 一个关于curl post提交返回数据的问题1
- C# post数据时 出现如下错误: System.Net.WebException: 远程服务器返回错误: (417) Expectation Failed 的解决办法
- Web页面接受客户端POST数据,并且返回数据
- android 通过get,post方式提交数据给服务器
- dojo中客户端向服务器提交数据