| 加入收藏| 设为首页| 联系我们

首页 站长学习 站长之家 源码下载 建站素材 书籍教程 常用工具
 您现在的位置: 动力中国 >> 网络编程 >> Ajax教程 >> 文章正文  
 ajax的server部分(php版)
 

ajax的server部分(php版)

http://www.domcn.org  文章来源:本站原创  点击数:

  关键字:ajax的server部分(php版)

 

Server端的任务通常是根据Client的请求,进行逻辑操作,并将结果响应返回。这个响应通常为XML格式(因此server端需要使用PHP的DOM创建XML响应)

1.PHP使用DOM创建XML响应,供client端的JS解析然后在页面中显示;(因此需要熟练PHP的DOM API)
其实,PHP生成XML的方法有两种:
使用DOM API;(方法一)
另一种是直接将XML的内容echo出去即可;(方法二)
见示例:
HTML页面(包含三个JS触发函数:onmouseover, onmouseout, onclick; 分别触发自己的函数)
<!doctype html public -//w3c//dtd html 4.0 tRANSITIONAL//en>
<html>
<head>
<title> Server PHP Ajax </title>
<script type=text/javascript src=js.js></script>
</head>

<body>
<span onmouseover=PHPechoXML() onmouseout=PHPDOMXML()>Default Words</span>
<div id=show></div>
divide<input type=text id=firstNumber/>by
<input type=text id=secondNumber/>
<input type=button value=Send onclick=CSparameter()/>
<div id=result></div>
</body>
</html>
JS页面(分别定义三个JS触发函数:PHPechoXML, PHPDOMXML, 以及CSparameter)
其中有XMLHttpRequest对象创建函数,以及各自的Server响应处理函数
///////1. 创建XMLHttpRequest对象
var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject()
...{
  var xmlHttp;

  try
  ...{
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  ...{
    // assume IE6 or older
    var XmlHttpVersions = new Array(MSXML2.XMLHTTP.6.0,
                                    MSXML2.XMLHTTP.5.0,
                                    MSXML2.XMLHTTP.4.0,
                                    MSXML2.XMLHTTP.3.0,
                                    MSXML2.XMLHTTP,
                                    Microsoft.XMLHTTP);
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
    ...{
      try
 
      ...{
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) ...{}
    }
  }
  if (!xmlHttp)
    alert(Error creating the XMLHttpRequest object.);
  else
    return xmlHttp;
}


///////2. JavaScript事件响应函数(onmouseover触发)
// read a file from the server
function PHPechoXML()
...{
  // only continue if xmlHttp isnt void
  if (xmlHttp)
  ...{
    // try to connect to the server
    try
    ...{
      // initiate reading a file from the server
      //向Server端的PHPechoXML.php文件发送异步请求
      xmlHttp.open(GET, PHPechoXML.php, true);
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    ...{
      alert(Cant connect to server: + e.toString());
    }
  }
}

///////3. JavaScript事件响应函数(onmouseout触发)
function PHPDOMXML()
...{
  // only continue if xmlHttp isnt void
  if (xmlHttp)
  ...{
    // try to connect to the server
    try
    ...{
      // initiate reading a file from the server
      //向Server端的PHPDOMXML.php文件发送异步请求
      xmlHttp.open(GET, PHPDOMXML.php, true);
      xmlHttp.onreadystatechange = handleRequestStateChange;
      xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    ...{
      alert(Cant connect to server: + e.toString());
    }
  }
}

// handles the response received from the server,Server端状态回调函数
function handleRequestStateChange()
...{

  if (xmlHttp.readyState == 4)
  ...{
    // continue only if HTTP status is OK
    if (xmlHttp.status == 200)
    ...{
      try
      ...{
          // read the message from the server
          var xmlResponse = xmlHttp.responseXML;

            //捕获IE和Opera潜在的错误
            if(!xmlResponse||!xmlResponse.documentElement)
            ...{
                throw(Invalid XML structure:  +xmlHttp.responseText);
            }
            //捕获FireFox的潜在错误
            var rootNodeName=xmlResponse.documentElement.nodeName;
            if(rootNodeName==parsererror)
            ...{
                throw(Invalid XML structure: +xmlHttp.responseText);
            }

            //获取Server端响应的XML响应并解析,到网页中显示
          // obtain the XMLs document element
          xmlRoot = xmlResponse.documentElement; 
          // obtain arrays with book titles and ISBNs
          cityArray=xmlRoot.getElementsByTagName(city);
          // generate HTML output
          var html = ; 
          // iterate through the arrays and create an HTML structure
          for (var i=0; i<cityArray.length; i++)
            html += cityArray.item(i).firstChild.data + <br/>;
          // obtain a reference to the <div> element on the page
          myDiv = document.getElementById(show);
          // display the HTML output
          myDiv.innerHTML = Server says: <br /> + html;
      }
      catch(e)
      ...{
        // display error message
        alert(Error reading the response: + e.toString());
      }
    }
    else
    ...{
      // display status message
      alert(There was a problem retrieving the data: +
            xmlHttp.statusText);
    }
  }
}

 

///////4. JavaScript事件响应函数(onclick触发)
function CSparameter()
...{
  // only continue if xmlHttp isnt void
  if (xmlHttp)
  ...{
    // try to connect to the server
    try
    ...{

    //获取form中的值
    var firstNumber=document.getElementById(firstNumber).value;
    var secondNumber=document.getElementById(secondNumber).value;

    //设置为参数,对Server端的CSparameter.php进行异步请求
   var param=firstNumber=+firstNumber+&secondNumber=+secondNumber;

      // initiate reading a file from the server
      xmlHttp.open(GET, CSparameter.php?+param, true);
      xmlHttp.onreadystatechange = handleRequestStateChangePara;
      xmlHttp.send(null);
    }
    // display the error in ca

[1] [2] [3] [4] 下一页


ajax的server部分(php版)
  • 上一篇文章:

  • 下一篇文章:
  •  热门文章
    普通文章 电子邮件改头换面 四公司畅谈未
    普通文章 PC病毒史上最声名狼藉的八大病
    普通文章 Rails系统中的AJAX开发技术简析
    普通文章 基于ASP.NET AJAX框架实现表单
    普通文章 开发ASP.NET AJAX客户端定制行
    普通文章 用JFreeChart对JSP报表进行增强
    普通文章 SQL Server 2005上的CLR和ADO.
    普通文章 SQL Server 2005的XML支持机制
    普通文章 Firefox中标签式浏览技巧大全
    普通文章 Tomcat中的Session和Cookie大揭
     
     推荐文章
    推荐文章 把Google地图嵌入网页 就是这么
    推荐文章 迅雷搜索候选资源出错的解决
    推荐文章 轻松去除迅雷里的各种广告和资
    推荐文章 突破限制 免费领养到QQ空间五级
    推荐文章 Rational统一过程RUP贴近中小软
    推荐文章 构建自己的轻量级XML DOM分析程
    推荐文章 WPS Office 2007技巧:妙用配置
    推荐文章 Excel 2007:求余数函数实用进阶
    推荐文章 浅谈ASP.NET的Postback
    推荐文章 软件开发中项目需求管理简述
     
     相关文章
    没有相关文章
    设为首页 | 加入收藏 | 广告合作 | 联系站长 | 版权申明 |
    动力中国为网友提供免费学习资料,可用资源,如果您认为我们的相关内容侵害到了您的权利请联系管理员
    Copyright © 2006-2008 domcn.org All Rights Reserved.