扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年10月2日
关键字:
在本页阅读全文(共2页)
  下面开始写发送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, "<T/>", "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(@"<time>GET:{0}</time>", 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(@"<time>POST:{0}</time>", System.DateTime.Now)); 
   xmlDoc.Save(context.Response.OutputStream); 
   context.Response.End(); 
   } 
   } 
   } 
   
   public bool IsReusable 
   { 
   get 
   { 
   return false; 
   } 
   } 
   } 
   
  http://www.cnblogs.com/Files/laiwen/XmlHttpNet.rar 
  http://www.cnblogs.com/laiwen/archive/2006/12/26/604050.html 
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。