• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

AJAX源码应用示例_jquery

jquery 搞代码 7年前 (2018-06-12) 165次浏览 已收录 0个评论

本示例主要演示如何操作XMLHttpRequest…..

XMLHttpRequest 简介

要真正实现这种绚丽的奇迹,必须非常熟悉一个 JavaScript 对象,即 XMLHttpRequest。这个小小的对象实际上已经在几种浏览器中存在一段时间了,它是本专栏今后几个月中要介绍的 Web 2.0、Ajax 和大部分其他内容的核心。为了让您快速地大体了解它,下面给出将要用于该对象的很少的几个 方法和属性。

  • open():建立到服务器的新请求。
  • send():向服务器发送请求。
  • abort():退出当前请求。
  • readyState:提供当前 html 的就绪状态。
  • responseText:服务器返回的请求响应文本。

客户端HTML代码(test.htm):

<script language=”javascript” type=”text/javascript”>
   var xmlHttp = false;
   try {
     xmlHttp = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);
     } catch (othermicrosoft) {
       try {
         xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
       } catch (failed) {
         xmlHttp = false;
       } 
     }
   }

   if (!xmlHttp)
     alert(“Error initializing XMLHttpRequest!”);

   function getCustomerInfo() {
     var phone = document.getElementById(“qq”).value;
     var url = “demo2.asp?qq=” + escape(phone);
     xmlHttp.open(“GET”, url, true);
     xmlHttp.onreadystatechange = updatePage;
     xmlHttp.send(null);
   }

   function updatePage() {
     if (xmlHttp.readyState == 4) {
   if (xmlHttp.status == 200) {
   var response = xmlHttp.responseText.split(“|”);
   document.getElementById(“message”).innerHTML = ‘号码是:’ + response[0] + ‘<br>姓名是:’ + response[1] + ‘<br>性别是:’ + response[2] + ‘<br>职务是:’ + response[3];
   alert(“响应服务完成!”);
   }
   else if (xmlHttp.status == 404) {
   alert(‘请求的网址不存在!’);
   }
   else {
   alert(‘错误:错误代码为:’ + xmlHttp.status);
   }
 }
}
</script>
<input id=”qq” type=”text” />
<div id=”message”>请尝试输入我的QQ号码:178010108,会看到返回的详细资料.</div>

服务端程序代码(demo2.asp):

<%
Response.ContentType = “text/xml”
Response.CharSet = “GB2312”

if request(“qq”) = “178010108” then
response.write “178010108|阿里西西|男|ASP技术,欢迎访问<a href=www.gaodaima.com>阿里西西</a>”
else
response.write “这个QQ号码是空号哦”
end if
%>

欢迎大家阅读《AJAX源码应用示例_jquery,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:AJAX源码应用示例_jquery

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址