'AjaxPro'未定義錯誤的原因&javascript順序執行&AjaxPro機制



轉載: 
呵呵,終於明白'AjaxPro'未定義錯誤的原因了!!
        先從javascript的執行方式說起吧!我用C#比較熟,在C#裏面任何一個變量或函數的位置是無關緊要的,比如說下面
public void add()
{
i++;
}
public void run()
{
add();
}
int i=0;
和下面的方法沒有區別
public void add()
{
i++;
}
int i=0;
public void run()
{
add();
}


但是在javascript裏面就不行了,如果你這麼寫
function add()
{
i++;
}
add();
var i=0;
這時候就會出錯!因爲在add裏面的i尚未定義(undefined)!所以說javascript的執行方式爲順序執行(暫且不說C#到底是不是順序執行!)
       再來說說AjaxPro的機制,
       首先我們說說AjaxPro.Utility.RegisterTypeForAjax到底做了什麼?
Ajax的基本原理其實很簡單,就是XmlHttpRequest通道加異步。AjaxPro起到就是框架和包裝器的作用,一般我們在on_load裏面用AjaxPro.Utility.RegisterTypeForAjax實現這些(當然這裏面AjaxPro.AjaxMethod()起到配合的作用),到底做了什麼呢?服務端就不說了(我估計用到了緩存方法),在客戶端aspx(html)頁面裏緊跟<form name="form1" method="post" action="Default3.aspx" id="form1">隔着VIEWSTATE生成了下面註冊腳本
<script type="text/javascript" src="/Web/ajaxpro/prototype.ashx"></script>
<script type="text/javascript" src="/Web/ajaxpro/core.ashx"></script>
<script type="text/javascript" src="/Web/ajaxpro/converter.ashx"></script>
<script type="text/javascript" src="/Web/ajaxpro/Default3,App_Web_wvuzlcxq.ashx"></script>
上面這些javascript就是AjaxPro實現了Ajax框架和通道的包,其中<script type="text/javascript" src="/Web/ajaxpro/core.ashx"></script>裏面有AjaxPro.onLoading的聲明!
        下面該說出現'AjaxPro'未定義錯誤的原因了!
在我這裏出現這種錯誤的情況是這樣的
Default3.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
     <title>無標題頁</title>
     <script type="text/javascript"> 
function get(r) 

document.getElementById("m").innerText=(r.value); 
}
AjaxPro.onLoading=function(b){ 
document.getElementById("loading").style.display = b ? "block" : "none"; 

</script> 
</head>
<body>
     <form id="form1" runat="server">
     <div>
     <div id="loading" style="display:none;">正在獲取服務器端時間
         </div>
             <div id="m" style="display:block;">
         </div>  
         <input id="but" type="button" value="button"  />


     </div>
     </form>
</body>
</html>
Default3.aspx.cs
。。。
public partial class Default3 : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
         AjaxPro.Utility.RegisterTypeForAjax(typeof(Default3));
     }
     [AjaxPro.AjaxMethod]
     //[AjaxPro.AjaxServerCache(10)]
     public string   gett()
     {
         System.Threading.Thread.Sleep(2000);
         return DateTime.Now.ToString();
     } 
}
你可以看到我把AjaxPro.onLoading放到了<form id="form1" runat="server">上面,也就是說源文件裏放到了
<script type="text/javascript" src="/Web/ajaxpro/prototype.ashx"></script>
<script type="text/javascript" src="/Web/ajaxpro/core.ashx"></script>
<script type="text/javascript" src="/Web/ajaxpro/converter.ashx"></script>
<script type="text/javascript" src="/Web/ajaxpro/Default3,App_Web_wvuzlcxq.ashx"></script>
的上面,由於javascript是順序執行的,所以就出現了'AjaxPro'未定義錯誤!
我把html文件改成下面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
     <title>無標題頁</title>
</head>
<body>
     <form id="form1" runat="server">
<script type="text/javascript"> 
function get(r) 

document.getElementById("m").innerText=(r.value); 
}
AjaxPro.onLoading=function(b){ 
document.getElementById("loading").style.display = b ? "block" : "none"; 

</script >
     <div>
     <div id="loading" style="display:none;">正在獲取服務器端時間
         </div>
             <div id="m" style="display:block;">
         </div>  
         <input id="but" type="button" value="button"  />


     </div>
     </form>
</body>
</html>
就行了!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章