日期:2014-05-16  浏览次数:20593 次

利用ajax实现局部刷新(简单的注册验证案例)
1,ajax(asynchronouse javascript and xml)异步的 javascrip 和xml
2,(包含了7种技术:javascript xml xstl dom xhtml css xmlhttpRequest)

3,是一种与服务器语言无关的技术,可以用在(php/jsp/asp.net)

4,ajax的工作原理: 创建一个ajax引擎->发送数据给服务器--》通过回调函数接收数据---》将数据赋给指定的页面

下面是注册验证案例register。php是注册页面。registerProcess.php用于接收数据并返回数据

//=====================================register.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用户注册</title>
    <!-- 告诉浏览器不要缓存 -->
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
<!--
function sendRequest(){
//创建一个ajax引擎
var xmlHttpRequest;
//不同浏览器获取xmlRequest对象方法不一样
if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("MMMMMictofrt");
}else{
xmlHttpRequest = new XMLHttpRequest();
}
return xmlHttpRequest;
}
var myXmlHttpRequest
function checkName(){
myXmlHttpRequest = sendRequest();
if(myXmlHttpRequest){
//window.alert("创建成功");
//第一个参数表示请求方式get.post。第二个参数指定url,对哪个页面发送ajax请求
//用get方式提交的数据如果没有发生变化的话浏览器将会从缓存中直接提取
var url = "/ajax_yhyz/registerProcess.php? username=getValue('username').value";
//1,用这样就可以不从缓存提取了
//var url = "/ajax_yhyz/registerProcess.php? mytime='new Date()' & username=getValue('username').value";
//2,<meta http-equiv="pragma" content="no-cache">告诉浏览器不要缓存
//window.alert(url);  用于测试url是否成功
//第三个参数ture 表示异步机制
myXmlHttpRequest.open("get",url,true);
//指定回调函数,chul是函数名,表示如果状态发生变化就调用该函数,有四个状态
myXml