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

來源:互聯網
上載者:User
'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>
就行了!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.