最近正在拜读<>这本书,运用书中知识,结合.net,写了这篇用.net 处理xmlHttp发送异步请求的文章。 我们要达到的目的是点击按钮,获得服务器的当前时间,aspx的html如下: 以下为引用的内容: Html <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Linkedu.Web.WebWWW.Default" %> 测试 要用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!"
|