Ajax 通用代码:
//名字空间对象
var net=new Object();
net.READY_STATE_UNINITALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;
//构造函数
net.ContentLoader=function(url,onload,onerror){
this.url=url;
this.req=null;
this.οnlοad=onload;
this.οnerrοr=(onerror)?onerror:defaultError;
this.loadXMLDoc(null);
}
net.ContentLoader.prototype={
loadXMLDoc:function(url){//重新命名的initXMLHttpRequest 函数
//重构过的loadXML函数
this.req=createXMLHttpRequests();
//重构过的SendRequest函数
if(this.req){
try{
var loader=this;
this.req.onreadystatechange=function(){
loader.onReadyState.call(loader);
}
this.req.open("GET",this.url,true);
this.req.send(null);
}catch(err){
this.onerror.call(this);
}
}
},
onReadyState:function(){ //重构过的回调函数
var req=this.req;
var ready=req.readyState;
if(ready==net.READY_STATE_COMPLETE){
var httpStatus=req.status
if(httpStatus==200||httpStatus==0){
this.onload.call(this);
}else{
this.onerror.call(this);
}
}
},
defaultError:function(){
alert("error fetching data!");
}
}
//创建XML对象
function createXMLHttpRequests()
{
var xmlHttp=false;
if(window.ActiveXObject) //IE浏览器
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
//document.frm.action="error.jsp";
//document.frm.submit();
}
}
}
else if(window.XMLHttpRequest) //基于Mozilla的浏览器 如Firefox
{
xmlHttp=new XMLHttpRequest();
if(xmlHttp.overrideMimeType)
{
xmlHttp.overrideMimeType("text/xml");
}
}
return xmlHttp;
}
PHP 调用的Page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Ajax函数测试</title>
<script language="JavaScript" type="text/javascript" src="ajaxnet.js"></script>
</head>
<body>
<script type="text/javascript">
function myCallBack(){
alert(this.url+" loaded! Here's the content:/n/n "+this.req.responseText);
}
function myCallError(){
alert("Error function/n/n:readyState"+this.req.readyState+"/n status:"+this.req.status+"/n n"+this.req.getAllResponseHeaders());
}
var url="demoServer.php"
var xmlHttp=false;
var loader=new net.ContentLoader(url,myCallBack,myCallError);
//常规调用
function createXMLHttpRequest()
{
//var xmlHttp=false;
if(window.ActiveXObject) //IE浏览器
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
//document.frm.action="error.jsp";
//document.frm.submit();
}
}
}
else if(window.XMLHttpRequest) //基于Mozilla的浏览器 如Firefox
{
xmlHttp=new XMLHttpRequest();
if(xmlHttp.overrideMimeType)
{
xmlHttp.overrideMimeType("text/xml");
}
}
//return xmlHttp;
}
//开始向服务器发送请求
function startRequest(url)
{
createXMLHttpRequest();
//判断xmlHttp是否创建成功
if(!xmlHttp)
{
//document.getElementById("tipsusername").innerHTML="创建异步请求失败!";
alert("创建异步请求失败!");
}
//alert("dd");
xmlHttp.onreadystatechange=processRequest;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
//处理请求
function processRequest()
{
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
processResult();
}
}
}
function processResult(){
alert(url+" loaded! Here's the content:/n/n "+xmlHttp.responseText);
}
startRequest(url);
</script>
</body>
</html>
//服务器端
<?php
header('Content-Type:text/xml; charset=gbk');
header("Cache-control:no-cache");
$data="<?xml version=/"1.0/" encoding=/"gbk/" ?>/n";
$data.="<result>/n";
$data.="<city id=/"120/">常州</city>";
$data.="</result>";
echo $data;
?>