Ajax 与PHP 调用 这个JS是很通用的。

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;   
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值