AjaxPro學習筆記

來源:互聯網
上載者:User

//頁面首次載入過程:
Utility.RegisterTypeForAjax :
將以下指令碼註冊:
<script type="text/javascript" src="/AjaxPro.Sample/ajaxpro/prototype.ashx"></script>
<script type="text/javascript" src="/AjaxPro.Sample/ajaxpro/core.ashx"></script>
<script type="text/javascript" src="/AjaxPro.Sample/ajaxpro/converter.ashx"></script>
<script type="text/javascript" src="/AjaxPro.Sample/ajaxpro/AjaxPro.Sample.Sample_1,AjaxPro.Sample.ashx"></script>
.net運行架構調用以下方法:
AjaxHandlerFactory.GetHandler
1.由於.ashx被註冊為自訂處理,此方法被調用4次;
2.依據請求類型(get)和請求的目標進行分別處理,分別對應EmbeddedJavaScriptHandler(2次),ConverterJavaScriptHandler(1次),TypeJavaScriptHandler(1次)
3..net運行架構在AjaxHandlerFactory.GetHandler返回不同的IHttpHandler實現(EmbeddedJavaScriptHandler等)時調用該實現的以下方法:
EmbeddedJavaScriptHandler.ProcessRequest方法被調用:
EmbeddedJavaScriptHandler.ProcessRequest方法被調用:
ConverterJavaScriptHandler.ProcessRequest方法被調用:
TypeJavaScriptHandler.ProcessRequest方法被調用:
向用戶端輸出以下代碼:
addNamespace("AjaxPro.Sample");
AjaxPro.Sample.Sample_1_class = Class.create();
AjaxPro.Sample.Sample_1_class.prototype = (new AjaxPro.AjaxClass()).extend(
{
              GetServerTime: function() 
              {
                            return this.invoke("GetServerTime", {}, this.GetServerTime.getArguments().slice(0));
              },
              AddTwo: function(firstInt, secondInt) 
              {
                            return this.invoke("AddTwo", {"firstInt":firstInt, "secondInt":secondInt}, this.AddTwo.getArguments().slice(2));
              },
              initialize: function() 
              {
                            this.url = '/AjaxPro.Sample/ajaxpro/AjaxPro.Sample.Sample_1,AjaxPro.Sample.ashx';
              }
});
AjaxPro.Sample.Sample_1 = new AjaxPro.Sample.Sample_1_class();
//分析prototype.js
var Class = {
              create: function() {
                            return function() {
                                                                      if(typeof this.initialize == "function")
                                                                      this.initialize.apply(this, arguments);
                                          }
                            }
}
AjaxPro.AjaxClass = Class.create();
AjaxPro.AjaxClass.prototype = {
              initialize: function(url) {
                            this.url = url;
              },
              invoke: function(method, args, e) {
                            if(e != null) {
                                          if(e.length != 6) for(;e.length<6;) e.push(null);
                                          if(e[2] == null) e[2] = this.onLoading;
                                          if(e[2] == null) e[3] = this.onError;
                                          if(e[2] == null) e[4] = this.onTimeout;
                                          if(e[2] == null) e[5] = this.onStateChanged;
                                          if(typeof e[0] == "function") {
                                                        return AjaxPro.queue.add(this.url, method, args, e);
                                          }
                            }
                            var r = new AjaxPro.Request();
                            r.url = this.url;
                            r.onLoading = this.onLoading;
                            r.onError = this.onError;
                            r.onTimeout = this.onTimeout;
                            r.onStateChanged = this.onStateChanged;
                            return r.invoke(method, args);
              }
};
AjaxPro.Request = Class.create();
AjaxPro.Request.prototype = {
              invoke: function(method, args, callback, context) {
                            this.__start = new Date().getTime();
                            this.isRunning = true;
                            this.method = method;
                            this.args = args;
                            this.callback = callback;
                            this.context = context;
  
                            if(MS.Debug.enabled == true)
                            MS.Debug.trace("Invoking " + method + "...");

                            var async = typeof callback == "function" && callback != AjaxPro.noOperation;
                            var json = AjaxPro.toJSON(args) + " ";

                            if(AjaxPro.cryptProvider != null)
                            json = AjaxPro.cryptProvider.encrypt(json);

                            if(async) {
                                                        this.xmlHttp.onreadystatechange = this.doStateChange.bind(this);
                                                        if(typeof this.onLoading == "function") this.onLoading(true);
                            }

                            this.xmlHttp.open("POST", this.url, async);
                            this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                            this.xmlHttp.setRequestHeader("Content-Length", json.length);
                            this.xmlHttp.setRequestHeader("Ajax-method", method);
  
                            if(AjaxPro.token != null && AjaxPro.token.length > 0)
                            this.xmlHttp.setRequestHeader("Ajax-token", AjaxPro.token);

                            if(MS.Browser.isIE)
                                                        this.xmlHttp.setRequestHeader("Accept-Encoding", "gzip, deflate");
                            else
                                                        this.xmlHttp.setRequestHeader("Connection", "close");  // Mozilla Bug #246651

                            if(this.onTimeout != null && typeof this.onTimeout == "function")
                            this.timeoutTimer = setTimeout(this.timeout.bind(this), AjaxPro.timeoutPeriod);

                            this.xmlHttp.send(json);
  
                            json = null;
                            args = null;
                            delete json;
                            delete args;
  
                            if(!async) {
                                                        return this.createResponse();
                            }
  
                            return true; 
              }
}
//當使用者觸發UI控制項動作時
1.由於產生的用戶端指令碼中包含對於"/AjaxPro.Sample/ajaxpro/AjaxPro.Sample.Sample_1,AjaxPro.Sample.ashx"的調用,導致AjaxHandlerFactory.GetHandler被調用
2.在AjaxHandlerFactory.GetHandler中返回AjaxSyncHttpHandler
3..net運行架構在AjaxHandlerFactory.GetHandler返回不同的IHttpHandler實現(AjaxSyncHttpHandler等)時調用該實現的以下方法:
AjaxSyncHttpHandler.ProcessRequest方法被調用:
將調用轉嫁到AjaxProcHelper.Run操作:
1.通過反射實現對指定C#類方法的調用.
2.將反射調用結果通過XmlHttpRequestProcessor.SerializeObject中對JavaScriptSerializer.Serialize的調用通過Response向用戶端返回. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.