知识大全 .NET处理xmlHttp发送异步请求

Posted 事件

篇首语:坚志而勇为,谓之刚。刚,生人之德也。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 .NET处理xmlHttp发送异步请求相关的知识,希望对你有一定的参考价值。

.NET处理xmlHttp发送异步请求  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  最近正在拜读<<Ajax in Action>>这本书 运用书中知识 结 写了这篇 处理xmlHttp发送异步请求的文章        我们要达到的目的是点击按钮 获得服务器的当前时间 aspx的如下

  <%@ Page Language= C#  AutoEventWireup= true  CodeBehind= Default aspx cs  Inherits= Linkedu Web WebWWW Default  %> <!DOCTYPE  PUBLIC  //W C//DTD XHTML   Transitional//EN   transitional dtd > < xmlns=  ><head runat= server ><title>测试</title><script language= javascript  src= javascript/prototype/extras array js ></script><script language= javascript  src= javascript/xmlHttp js ></script><script language= javascript  src= javascript/eventRouter js ></script><script language= javascript  src= Default js ></script><script language= javascript > </script></head><body><form id= form  runat= server >用Post方式获得服务器的当前时间<input id= btnTestPost  type= button  value= Post  />用Get方式获得服务器的当前时间<input id= btnTestGet  type= button  value= Get  /><div id= divResult ></div></form></body></>

  要用javascript 发送xmlHttp 请求必须解决的问题是跨浏览器的支持 我们把xmlHttp的发送封装在一个javascript对象中 同时在这个对象中解决了跨浏览器支持的问题 代码如下

  /*url loading object and a request queue built on top of it*/ /**//* namespacing object */ var net=new Object(); net READY_STATE_UNINITIALIZED= ;net READY_STATE_LOADING= ;net READY_STATE_LOADED= ;net READY_STATE_INTERACTIVE= ;net READY_STATE_PLETE= ;

  /**//*  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 form urlencoded ;if (window XmlHttpRequest)this req=new XmlHttpRequest(); else if (window ActiveXObject)this req=new ActiveXObject( Microsoft xmlHttp );if (this req)tryvar 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_PLETE)var ;if (Status==  || Status== )this onload call(this);elsethis 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());

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

  //全局xmlHttp对象var cobj; /**//* Post begin*///绑定Post发送xmlHttp事件到btnTestPostfunction 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  onerrorcobj = new net xmlHttp( DefaultHandler ashx dealResult   <T/>   POST );/**//* Post end*//**//* Get begin*///绑定Get发送xmlHttp事件到btnTestGetfunction 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  onerrorcobj = new net xmlHttp( DefaultHandler ashx?T= dealResult  null   GET );/**//* Get end*/ function dealResult() var dobj = document getElementById( divResult );dobj innerHTML = cobj req responseXML text;window onload = function()//绑定Post发送xmlHttp事件到btnTestPostloadTestPost();//绑定Get发送xmlHttp事件到btnTestGetloadTestGet();;

  最后处理xmlHttp的代码

cha138/Article/program/net/201311/12276

相关参考