'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>
就行了!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章