Ajax底层代码简析(可直接用的框架)

        近来忙毕业设计,又很长时间没写blog了。
        学ajax也有段时间了,理论是看了不少,也对MagicAjax框架做了下了解,当然要吃透它还是有很长的路要走。
        一直觉得对Ajax底层的代码应该总结一下。其实很底层的代码是比较简洁明了的,但功能却比较简单。一般我们都用Send(null).以前我一直在想我怎么来控制当异步传送Http请求时要调用的后台的指定的(我想用的)代码,现在终于明白了(唉,对自己的智商表示怀疑),那就要在Send方法中传送参数。参数中应该包括事件的触发者和它的参数,还有就是ViewState的值。
        具体代码如下(里面我加了些注释,后面不再解释):

  1 None.gif __PageForm  =   null ;
  2 None.gif
  3 None.gif function  AjaxCallObject()
  4 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  5InBlock.gif    this.Init();
  6ExpandedBlockEnd.gif}

  7 None.gif
  8 None.gifAjaxCallObject.prototype.Init  =   function ()
  9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 10InBlock.gif    this.XmlHttp = this.GetHttpObject();
 11ExpandedBlockEnd.gif}

 12 None.gif 
 13 None.gifAjaxCallObject.prototype.GetHttpObject  =   function ()
 14 ExpandedBlockStart.gifContractedBlock.gif dot.gif
 15InBlock.gif    var xmlhttp;
 16InBlock.gif    if (!xmlhttp) 
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 18InBlock.gif        try 
 19ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 20InBlock.gif            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 21ExpandedSubBlockEnd.gif        }
 
 22InBlock.gif        catch (e) 
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif            xmlhttp = false;
 25ExpandedSubBlockEnd.gif        }

 26ExpandedSubBlockEnd.gif    }

 27InBlock.gif    return xmlhttp;
 28ExpandedBlockEnd.gif}

 29 None.gif
 30 None.gifAjaxCallObject.prototype.HookAjaxCall  =   function ()
 31 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 32InBlock.gif    __PageForm = document.getElementById('Form1');
 33InBlock.gif    if (typeof __doPostBack != 'undefined')
 34InBlock.gif        __doPostBack = this.DoPostBack;
 35ExpandedBlockEnd.gif}

 36 None.gif
 37 None.gif //  Replaces normal __doPostBack
 38 None.gif AjaxCallObject.prototype.DoPostBack  =   function (eventTarget, eventArgument)
 39 ExpandedBlockStart.gifContractedBlock.gif dot.gif {    
 40InBlock.gif    AJAXCbo.DoAjaxCall(eventTarget, eventArgument);
 41ExpandedBlockEnd.gif}

 42 None.gif 
 43 None.gifAjaxCallObject.prototype.DoAjaxCall  =   function (eventTarget, eventArgument)
 44 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 45InBlock.gif    var theData = '';
 46InBlock.gif    var theform = __PageForm;
 47InBlock.gif    
 48InBlock.gif    if(theform == null)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 50InBlock.gif        return;
 51ExpandedSubBlockEnd.gif    }

 52InBlock.gif    
 53InBlock.gif    var thePage = theform.action;//得到页面地址
 54InBlock.gif    var eName = '';
 55InBlock.gif
 56InBlock.gif    theData  = '__EVENTTARGET='  + this.EncodePostData(eventTarget.split("$").join(":")) + '&';//得到事件的发起者,也就是object Sender
 57InBlock.gif    theData += '__EVENTARGUMENT=+ this.EncodePostData(eventArgument) + '&';//得到参数,就是EventArgs e
 58InBlock.gif
 59InBlock.gif    var elemCount = theform.elements.length;
 60InBlock.gif    forvar i=0; i<elemCount; i++ )
 61ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 62InBlock.gif        curElem = theform.elements[i];
 63InBlock.gif        eName = curElem.name;
 64InBlock.gif        if( eName && eName != '' && curElem.tagName != "EMBED")
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            if( eName == '__EVENTTARGET' || eName == '__EVENTARGUMENT' )
 67ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{}
 68InBlock.gif            else
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 70InBlock.gif                var type = curElem.type;
 71InBlock.gif                var val = curElem.value;
 72InBlock.gif
 73InBlock.gif                if ( type == "submit" || type == "button" )
 74InBlock.gif                    continue;
 75InBlock.gif
 76InBlock.gif                val = this.EncodePostData(val);
 77InBlock.gif
 78InBlock.gif                if ( type == "select-multiple" || type == "select-one" )
 79ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 80InBlock.gif                    var selectLength = curElem.options.length;
 81InBlock.gif                    var optNameStr = this.EncodePostData(eName);
 82InBlock.gif                    for (var j=0; j < selectLength; j++)
 83InBlock.gif                        if (curElem.options[j].selected)
 84InBlock.gif                            theData = theData + optNameStr + '=+ this.EncodePostData(curElem.options[j].value) + '&';
 85ExpandedSubBlockEnd.gif                }

 86InBlock.gif                else if ( (type != "checkbox" && type != "radio"|| curElem.checked )
 87ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 88InBlock.gif                    theData = theData + this.EncodePostData(eName) + '=+ val + '&';
 89ExpandedSubBlockEnd.gif                }

 90ExpandedSubBlockEnd.gif            }

 91ExpandedSubBlockEnd.gif        }

 92ExpandedSubBlockEnd.gif    }

 93InBlock.gif  
 94InBlock.gif    if (theData.substr(theData.length-1== "&")
 95InBlock.gif        theData = theData.substr(0, theData.length-1);
 96InBlock.gif        //alert(theData);
 97InBlock.gif        
 98InBlock.gif    ifthis.XmlHttp )
 99ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{        
100InBlock.gif         //注释的是旧的代码,有问题,回送回来的状态都是0,原因还不明
101ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*AJAXCbo = new AjaxCallObject();
102InBlock.gif
103InBlock.gif        if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
104InBlock.gif        {
105InBlock.gif            // Asynchronous
106InBlock.gif            this.XmlHttp.open("POST", thePage, true);
107InBlock.gif            this.XmlHttp.onreadystatechange = function(){ AJAXCbo.ReadyStateChange(); };
108InBlock.gif            this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
109InBlock.gif            this.XmlHttp.send(theData);
110ExpandedSubBlockEnd.gif        }*/

111InBlock.gif        var oThis = this;
112InBlock.gif        AJAXCbo = new AjaxCallObject();
113InBlock.gif
114InBlock.gif        ifthis.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
115ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
116InBlock.gif            ajaxCallType = "async";
117InBlock.gif        
118InBlock.gif            if ( ! ajaxCallType || ajaxCallType.toLowerCase() != "sync")
119ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
120InBlock.gif                // 异步传输
121InBlock.gif                this.XmlHttp.open("POST", thePage, true);
122ExpandedSubBlockStart.gifContractedSubBlock.gif                this.XmlHttp.onreadystatechange = function()dot.gif{ oThis.ReadyStateChange(); };
123InBlock.gif                this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
124InBlock.gif                this.XmlHttp.send(theData);
125ExpandedSubBlockEnd.gif            }

126InBlock.gif            else
127ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
128InBlock.gif                // 同步传输,页面会刷新
129InBlock.gif                window.setTimeout(
130InBlock.gif                    function()
131ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
132InBlock.gif                        oThis.XmlHttp.open("POST", thePage, false);
133InBlock.gif                        oThis.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
134InBlock.gif                        oThis.XmlHttp.send(theData);
135InBlock.gif
136InBlock.gif                        if( oThis.XmlHttp.status == 200 && oThis.XmlHttp.statusText == "OK" )
137InBlock.gif                            oThis.OnComplete(oThis.XmlHttp.responseText, oThis.XmlHttp.responseXML);
138InBlock.gif                        else
139InBlock.gif                            oThis.OnError(oThis.XmlHttp.status, oThis.XmlHttp.statusText, oThis.XmlHttp.responseText);
140ExpandedSubBlockEnd.gif                    }
1);
141ExpandedSubBlockEnd.gif            }
            
142ExpandedSubBlockEnd.gif        }

143ExpandedSubBlockEnd.gif    }

144InBlock.gif    return true;
145ExpandedBlockEnd.gif}

146 None.gif
147 ExpandedBlockStart.gifContractedBlock.gif /**/ /*AjaxCallObject.prototype.OnLoading = function()
148InBlock.gif{
149InBlock.gif  // Loading
150InBlock.gif}
151InBlock.gif
152InBlock.gifAjaxCallObject.prototype.OnLoaded = function()
153InBlock.gif{
154InBlock.gif  // Loaded
155InBlock.gif}
156InBlock.gif
157InBlock.gifAjaxCallObject.prototype.OnInteractive = function()
158InBlock.gif{
159InBlock.gif  // Interactive
160InBlock.gif}
161ExpandedBlockEnd.gif*/

162 None.gifAjaxCallObject.prototype.OnComplete  =   function (responseText, responseXml)
163 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
164InBlock.gif    this.SetHtmlOfPage(responseText);
165InBlock.gif    return true;
166ExpandedBlockEnd.gif}

167 None.gif
168 ExpandedBlockStart.gifContractedBlock.gif /**/ /*AjaxCallObject.prototype.OnAbort = function()
169ExpandedBlockEnd.gif{}*/

170 None.gif
171 None.gifAjaxCallObject.prototype.OnError  =   function (status, statusText, responseText)
172 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
173InBlock.gif    if (status==200)
174ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
175InBlock.gif        // a weird bug of Opera sometimes invokes OnError when there's no error
176InBlock.gif        this.OnComplete(responseText);
177InBlock.gif        return;
178ExpandedSubBlockEnd.gif    }

179InBlock.gif
180InBlock.gif    document.close();    // for IE
181InBlock.gif    document.write(responseText);
182InBlock.gif    document.close();    // for Firefox
183ExpandedBlockEnd.gif}

184 None.gif
185 None.gifAjaxCallObject.prototype.ReadyStateChange  =   function ()
186 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
187ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*if( this.XmlHttp.readyState == 1 )
188InBlock.gif    {
189InBlock.gif        this.OnLoading();
190InBlock.gif    }
191InBlock.gif    else if( this.XmlHttp.readyState == 2 )
192InBlock.gif    {
193InBlock.gif        this.OnLoaded();
194InBlock.gif    }
195InBlock.gif    else if( this.XmlHttp.readyState == 3 )
196InBlock.gif    {
197InBlock.gif        this.OnInteractive();
198InBlock.gif    }
199ExpandedSubBlockEnd.gif    else */

200InBlock.gif    //alert("ly");
201InBlock.gif    //alert(this.XmlHttp.readyState);
202InBlock.gif    ifthis.XmlHttp.readyState == 4)
203ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
204ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//*if( this.XmlHttp.status == 0 )
205InBlock.gif            this.OnAbort();
206ExpandedSubBlockEnd.gif        else*/

207InBlock.gif        ifthis.XmlHttp.status == 200 )
208ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
209InBlock.gif            this.OnComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML);
210ExpandedSubBlockEnd.gif        }

211InBlock.gif        else
212ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
213InBlock.gif            this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);   
214ExpandedSubBlockEnd.gif        }

215ExpandedSubBlockEnd.gif    }

216ExpandedBlockEnd.gif}

217 None.gif
218 None.gifAjaxCallObject.prototype.EncodePostData  =   function (data)
219 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
220InBlock.gif    return data.split("%").join("%25").split("=").join("%3d").split("&").join("%26").split("+").join("%2b");
221ExpandedBlockEnd.gif}

222 None.gif
223 None.gifAjaxCallObject.prototype.SetHtmlOfPage  =   function (html)
224 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
225InBlock.gif    document.close();    // for IE
226InBlock.gif    document.write(html);//用新的html代码替代页面上的html代码
227InBlock.gif    document.close();    // for Firefox
228ExpandedBlockEnd.gif}

229 None.gif
230 None.gif var  AJAXCbo  =   new  AjaxCallObject();
        代码长了点,不过可以直接用了,只要在相应的后台代码中给服务器控件加段代码就成了。比如说,Web页面上放了个按钮控件,ID为Button1,那么在后台代码的Page_Load中加上下面一段代码:
None.gif Button1.Attributes.Add( " onclick " , " modify(); " );
这里的modify是我在页面的html里定义的一个方法,其内容是:
None.gif < script >
None.gif            
function  modify()
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
if(typeof(AJAXCbo) == 'undefined')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
var AJAXCbo = new AjaxCallObject();
ExpandedSubBlockEnd.gif                }

InBlock.gif                AJAXCbo.HookAjaxCall();
ExpandedBlockEnd.gif            }

None.gif        
</ script >
         到这就结束了,你可以看到无刷新更新的效果了。接下来的工作就是想办法更进一步,只把更新过的值传到相应的控件,该怎么做还需要继续努力了,呵呵。
        水平有限,难免有错误,望能和大家多多交流!
 例子代码下载: /Files/fxb248/ajaxtestaaa.rar

转载于:https://www.cnblogs.com/fxb248/archive/2006/05/25/408780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值