当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  用.net 处理xmlHttp发送异步请求 1

 用.net 处理xmlHttp发送异步请求 1

点击次数:18 次 发布日期:2008-11-26 11:32:14 作者:源代码网
源代码网推荐      最近正在拜读<<Ajax in Action>>这本书,运用书中知识,结合.net,写了这篇用.net 处理xmlHttp发送异步请求的文章。
源代码网推荐  
源代码网推荐  我们要达到的目的是点击按钮,获得服务器的当前时间,aspx的html如下:
源代码网推荐  Html
源代码网推荐  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Linkedu.Web.WebWWW.Default" %>
源代码网推荐  
源代码网推荐  <!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 runat="server">
源代码网推荐   <title>测试</title>
源代码网推荐   <script language="javascript" src="http://www.zzchn.com/edu/20080727/javascript/prototype/extras-array.js"></script>
源代码网推荐   <script language="javascript" src="http://www.zzchn.com/edu/20080727/javascript/xmlHttp.js"></script>
源代码网推荐   <script language="javascript" src="http://www.zzchn.com/edu/20080727/javascript/eventRouter.js"></script>
源代码网推荐   <script language="javascript" src="http://www.zzchn.com/edu/20080727/Default.js"></script>
源代码网推荐   <script language="javascript">
源代码网推荐  
源代码网推荐   </script>
源代码网推荐  </head>
源代码网推荐  <body>
源代码网推荐   <form id="form1" runat="server">
源代码网推荐   用Post方式获得服务器的当前时间
源代码网推荐   <input id="btnTestPost" type="button" value="Post" />
源代码网推荐   用Get方式获得服务器的当前时间
源代码网推荐   <input id="btnTestGet" type="button" value="Get" />
源代码网推荐   <div id="divResult"></div>
源代码网推荐  </form>
源代码网推荐  </body>
源代码网推荐  </html>
源代码网推荐  
源代码网推荐  要用javascript 发送xmlHttp 请求必须解决的问题是跨浏览器的支持。我们把xmlHttp的发送封装在一个javascript对象中,同时在这个对象中解决了跨浏览器支持的问题。代码如下:
源代码网推荐  
源代码网推荐  xmlHttp对象
源代码网推荐  /**//*
源代码网推荐  url-loading object and a request queue built on top of it
源代码网推荐  */
源代码网推荐  
源代码网推荐  /**//* namespacing object */
源代码网推荐  var net=new Object();
源代码网推荐  
源代码网推荐  net.READY_STATE_UNINITIALIZED=0;
源代码网推荐  net.READY_STATE_LOADING=1;
源代码网推荐  net.READY_STATE_LOADED=2;
源代码网推荐  net.READY_STATE_INTERACTIVE=3;
源代码网推荐  net.READY_STATE_COMPLETE=4;
源代码网推荐  
源代码网推荐  
源代码网推荐  /**//*--- content loader object for cross-browser requests ---*/
源代码网推荐  net.xmlHttp=function(url, onload, params, method, contentType, onerror){
源代码网推荐   this.req=null;
源代码网推荐   this.onload=onload;
源代码网推荐   this.onerror=(onerror) ? onerror : this.defaultError;
源代码网推荐   if(typeof(method) == "undefined" || method == null)
源代码网推荐   {
源代码网推荐   method = "POST";
源代码网推荐   }
源代码网推荐   this.loadXMLDoc(url, params, method, contentType);
源代码网推荐  }
源代码网推荐  
源代码网推荐  net.xmlHttp.prototype.loadXMLDoc=function(url, params, method, contentType){
源代码网推荐   if (!method){
源代码网推荐   method="GET";
源代码网推荐   }
源代码网推荐   if (!contentType && method=="POST"){
源代码网推荐   contentType="application/x-www-form-urlencoded";
源代码网推荐   }
源代码网推荐   if (window.XmlHttpRequest){
源代码网推荐   this.req=new XmlHttpRequest();
源代码网推荐   } else if (window.ActiveXObject){
源代码网推荐   this.req=new ActiveXObject("Microsoft.xmlHttp");
源代码网推荐   }
源代码网推荐   if (this.req){
源代码网推荐   try{
源代码网推荐   var loader=this;
源代码网推荐   this.req.onreadystatechange=function(){
源代码网推荐   net.xmlHttp.onReadyState.call(loader);
源代码网推荐   }
源代码网推荐   this.req.open(method,url,true);
源代码网推荐   if (contentType){
源代码网推荐   this.req.setRequestHeader("Content-Type", contentType);
源代码网推荐   }
源代码网推荐   this.req.send(params);
源代码网推荐   }catch (err){
源代码网推荐   this.onerror.call(this);
源代码网推荐   }
源代码网推荐   }
源代码网推荐  }
源代码网推荐  
源代码网推荐  
源代码网推荐  net.xmlHttp.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);
源代码网推荐   }
源代码网推荐   }
源代码网推荐  }
源代码网推荐  
源代码网推荐  net.xmlHttp.prototype.defaultError=function(){
源代码网推荐   alert("error fetching data!"
源代码网推荐   +" readyState:"+this.req.readyState
源代码网推荐   +" status: "+this.req.status
源代码网推荐   +" headers: "+this.req.getAllResponseHeaders());
源代码网推荐  }
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华