首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
ASP | ASP.NET | JSP | PHP | AJAX | XML | Java script | HTML/CSS | 服务器类
各大城市软件开发培训、软件人才免费咨询热线:400-700-5807
 您现在的位置: 中国IT实验室 >> WEB开发 >> AJAX学习教程 >> 正文
节选:快速进入AJAX开发



开始行动——Quickstart AJAX

1. 在附录A中,指导你建立一个Web服务器并创建一个名为ajax的Web文件夹。在这个文件夹里存放了这本书的代码。
1.        在ajax目录下创建一个名为quickstart的子目录。
2.        在quickstart目录下,创建一个名为index.html的文件,在文件中添加如下代码:

<!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>
      <title>AJAX with PHP: Quickstart</title>
      <script type="text/javascript" src="quickstart.js"></script>
   </head>
   <body onload='process()'>
      Server wants to know your name:
      <input type="text" id="myName" />
      <div id="divMessage" />
   </body>
</html>


3.        创建一个名为quickstart.js的文件,并添加如下代码:
// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();


// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject()
{
   // will store the reference to the XMLHttpRequest object
   var xmlHttp;
   // if running Internet Explorer
   if(window.ActiveXObject)
   {
      try
      {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
         xmlHttp = false;
      }
   }
   // if running Mozilla or other browsers
   else
   {
      try
      {
         xmlHttp = new XMLHttpRequest();
      }
      catch (e)
      {
         xmlHttp = false;
      }
   }
   // return the created object or display an error message
   if (!xmlHttp)
      alert("Error creating the XMLHttpRequest object.");
   else
      return xmlHttp;
}


// make asynchronous HTTP request using the XMLHttpRequest object
function process()
{
   // proceed only if the xmlHttp object isn't busy
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
   {
      // retrieve the name typed by the user on the form
      name = encodeURIComponent(document.getElementById("myName").value);
      // execute the quickstart.php page from the server
      xmlHttp.open("GET", "quickstart.php?name=" + name, true);
      // define the method to handle server responses
      xmlHttp.onreadystatechange = handleServerResponse;
      // make the server request
      xmlHttp.send(null);
   }
   else
      // if the connection is busy, try again after one second
      setTimeout('process()', 1000);
}


// executed automatically when a message is received from the server
function handleServerResponse()
{
   // move forward only if the transaction has completed
   if (xmlHttp.readyState == 4)
   {
      // status of 200 indicates the transaction completed successfully
      if (xmlHttp.status == 200)
      {
         // extract the XML retrieved from the server
         xmlResponse = xmlHttp.responseXML;
         // obtain the document element (the root element) of the XML structure
         xmlDocumentElement = xmlResponse.documentElement;
         // get the text message, which is in the first child of
         // the the document element
         helloMessage = xmlDocumentElement.firstChild.data;
         // update the client display using the data received from the server
 document.getElementById("divMessage").innerHTML ='<i>' + helloMessage + '</i>';
         // restart sequence
         setTimeout('process()', 1000);
      }
      // a HTTP status different than 200 signals an error
      else
      {
         alert("There was a problem accessing the server: " + xmlHttp.statusText);
      }
   }
}


4.        创建一个名为qucikstart.php的文件,并添加如下代码:
<?php
// we'll generate XML output
header('Content-Type: text/xml');
// generate XML header
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
// create the <response> element
echo '<response>';
// retrieve the user name
$name = $_GET['name'];
// generate output depending on the user name received from client
$userNames = array('CRISTIAN', 'BOGDAN', 'FILIP', 'MIHAI', 'YODA');
if (in_array(strtoupper($name), $userNames))
   echo 'Hello, master ' . htmlentities($name) . '!';
else if (trim($name) == '')
   echo 'Stranger, please tell me your name!';
else
   echo htmlentities($name) . ', I don\'t know you!';
// close the <response> element
echo '</response>';
?>



5.        现在你就可以使用你喜欢的浏览器,通过装载http://localhost/ajax/quickstart来访问新写的程序。装载这个页面,你将得到像图1和图2那样的页面。如果你在运行这个程序的过程中遇到任何问题,检查你是否按照附录A中描述的那样安装和配置。大多数错误的发生都是诸如输入之类的小错误。

 

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

中国IT教育热线咨询
相关文章
没有相关文章
最新文章
·PHP正则表达式从url中取得域名
·php设计模式介绍之迭代器模式
·简单学习php遇到的主要问题
·asp根据表单自动生成sql语句的函
·雅虎选项卡特效
 文章评论

 精彩友情推荐
·Asp源码 PHP源码
·CGI源码 JSP源码
·建站书籍教程
·服务器软件 .net源码
·建站工具软件
·IDC资讯大全
·机房品质万里行
·IDC托管必备知识
·全国IDC报价
·网站推广优化
ASP.NET ASP PHP JSP
·extjs ComboBox联动下拉菜单示例08-01
·漫谈.Net开发关于命名空间和目录划分07-31
·在Silverlight应用程序中操作Cookie07-28
·带附加条件的NewID()用法(downmoon)07-28
·对自定义路由进行单元测试07-28
·javascript实现yield07-28
·在ASP.NET中使用Google Maps07-28
·Sql Server2005 实现Oracle10g的hash表分区功07-28
·asp.net get set用法07-26
·Asp.net 控件开发—数据回传07-26
·接口vs. 的实体类07-26
·php设计模式介绍之迭代器模式08-02
·简单学习php遇到的主要问题08-02
·asp根据表单自动生成sql语句的函数08-02
·教你优化你的ASP程序03-07
·asp去除HTML标记的三个实用函数03-07
·ASP添加验证码的解决方法03-07
·ASP通用文章分页函数:非记录集分页03-07
·ASP教程基础:十天学会ASP第三天03-07
·ASP教程基础:十天学会ASP第二天03-07
·ASP教程基础:十天学会ASP第一天03-07
·能够生成google xml地图的asp源码03-06
·Linux系统下让PHP提高性能的工具APC05-06
·一个完整、安全的PHP用户登录系统11-14
·Apache+PHP+MySQL建立数据库驱动的动态网站08-24
·用SSH与PHP相连接 确保数据传输的安全性08-23
·PHP5手动最简安装方法08-03
·PHP程序加速探索之服务器负载测试07-11
·完全讲解PHP+MySQL的分页显示示例分析05-30
·用Suhosin加强PHP脚本语言安全性05-26
·初学入门 PHP 和 MySQL05-17
·传奇的诞生 PHP三位创始人简介05-10
·大型系统上PHP令人不爽的九大原因05-10
·ASP.NET和PHP、JSP究竟学哪个?07-30
·JAVA (Jsp)利用Google的Translate开发API07-29
·由Servlet获得FacesContext及ManagedBeans07-24
·用JOTM向Servlet中添加事务07-18
·用servlet生成验证码07-16
·JSP/Servlet伪静态网页实现07-08
·JSP和Servlet的关系浅谈06-15
·妙用异步Servlet扩展AJAX应用程序06-11
·servlet生成验证码图片06-02
·java.servlet.Filter的应用05-30
·Java程序员必看--扩展鼠标右键菜单功能05-13
  培训中心
人才交流中心 技术交流中心
  ITLab技术交流平台: