唐山网站建设

设为主页 加入收藏 繁體中文

用.net 处理xmlHttp发送异步要求

核心提示:最近正在拜读<>这本书,应用书中知识,结合.net,写了这篇用.net 处理xmlHttp发送异步要求的文章...

最近正在拜读<>这本书,应用书中知识,结合.net,写了这篇用.net 处理xmlHttp发送异步要求的文章。
  
我们要到达的目的是点击按钮,取得服务器确当前时间,aspx的html以下: 
  
以下为援用的内容:

Html
  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Linkedu.Web.WebWWW.Default" %>
  
 
  
 
 
   测试
  
  
  
  
  
 
 
  


   用Post方式取得服务器确当前时间
  
   用Get方式取得服务器确当前时间
  
  

 

 
 

  
要用javascript 发送xmlHttp 要求必须解决的题目是跨浏览器的支持。我们把xmlHttp的发送封装在1个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!"
   +"\n\nreadyState:"+this.req.readyState
   +"\nstatus: "+this.req.status
   +"\nheaders: "+this.req.getAllResponseHeaders());
  }

1 2 下1页

核心提示:最近正在拜读<>这本书,应用书中知识,结合.net,写了这篇用.net 处理xmlHttp发送异步要求的文章...

下面开始写发送xmlHttp要求的代码:

以下为援用的内容: 
  default.js
  //全局xmlHttp对象
  var cobj;
  
  /**//* Post begin*/
  //绑定Post发送xmlHttp事件到btnTestPost
  function loadTestPost()
  {
   var iobj = document.getElementById("btnTestPost");
   //btnTestPost按钮监听的绑定
   var clickRouter=new jsEvent.EventRouter(iobj,"onclick");
   clickRouter.addListener(btnTestPostClick);
  }
  function btnTestPostClick()
  { // open参数 url, onload, params, method, contentType, onerror
   cobj = new net.xmlHttp("DefaultHandler.ashx",dealResult, "", "POST");
  }
  /**//* Post end*/
  
  
  /**//* Get begin*/
  //绑定Get发送xmlHttp事件到btnTestGet
  function loadTestGet()
  {
   var iobj = document.getElementById("btnTestGet");
   //btnTestGet按钮监听的绑定
   var clickRouter=new jsEvent.EventRouter(iobj,"onclick");
   clickRouter.addListener(btnTestGetClick);
  }
  function btnTestGetClick()
  { // open参数 url, onload, params, method, contentType, onerror
   cobj = new net.xmlHttp("DefaultHandler.ashx?T=1",dealResult, null, "GET");
  }
  /**//* Get end*/
  
  
  
  function dealResult()
  {
   var dobj = document.getElementById("divResult");
   dobj.innerHTML = cobj.req.responseXML.text;
  }
  
  
  window.onload = function()
  {
   //绑定Post发送xmlHttp事件到btnTestPost
   loadTestPost();
   //绑定Get发送xmlHttp事件到btnTestGet
   loadTestGet();
  };
  
  最后是.net处理xmlHttp的代码
  .net 处理xmlHttp要求
  public class DefaultHandler : IHttpHandler
   {
   protected XmlDocument _xmlResult;
  
   public void ProcessRequest(HttpContext context)
   {
   if (context.Request["T"] != null)
   {//GET xmlhttp测试
   context.Response.ContentType = "text/xml";
   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.LoadXml(string.Format(@"", System.DateTime.Now));
   xmlDoc.Save(context.Response.OutputStream);
   context.Response.End();
   }
   else
   {//POST xmlhttp测试
   context.Response.ContentType = "text/xml";
   XmlDocument xmlDoc = new XmlDocument();
   xmlDoc.Load(context.Request.InputStream);
   if (xmlDoc.DocumentElement.Name == "T")
   {
   xmlDoc.LoadXml(string.Format(@"", System.DateTime.Now));
   xmlDoc.Save(context.Response.OutputStream);
   context.Response.End();
   }
   }
   }
  
   public bool IsReusable
   {
   get
   {
   return false;
   }
   }
   }

 

上1页 1 2 http://www.fw8.net/


TAG:事件,服务器,代码,按钮,绑定
评论加载中...
内容:
评论者: 验证码: