QuickGuide for AJAX[簡譯AJAX快速指南]以及對現有WebService的擴充。

來源:互聯網
上載者:User

AJAX .Net Wrapper quick usage guide
Karl Seguin - http://www.openmymind.net/ - copyright 2005

(view AjaxGuide.doc for more detailed information)

Step 1 -
   Create a reference to the ajax.dll file

Step 2 - Set up the HttpHandler
In the web.config, set up the HttpHandler, like:

<configuration>
  <system.web>
    <httpHandlers>
    <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
    </httpHandlers> 
    ...
  <system.web>
</configuration>

Step 3 -
In the Page_Load event, call the following function:

'vb.net
Public Class Index
  Inherits System.Web.UI.Page

  Private Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Ajax.Utility.RegisterTypeForAjax(GetType(Index))
  '...
  end sub
  '...
End Class

//C#
public class Index : System.Web.UI.Page{
   private void Page_Load(object sender, EventArgs e){
      Ajax.Utility.RegisterTypeForAjax(typeof(Index));     
      //... 
   }
   //... 
}

 

Step 4 -
In the codebehind of your page, add functions (like you normally would) that you'd like to be able to asynchrounsly called by client-side scripting.  Mark these functions with the Ajax.JavascriptMethodAttribute():

//C#
[Ajax.AjaxMethod()]
public int ServerSideAdd(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}

'VB.Net
<Ajax.AjaxMethod()> _
Public Function ServerSideAdd(ByVal firstNumber As Integer, ByVal secondNumber As Integer) As Integer
 Return firstNumber + secondNumber
End Function

The wrapper will automatically create a JavaScript function named "ServerSideAdd" which accepts to parameters.  When called, this server-side function will be called and the result returned.

Step 5 -
Using JavaScript, you can invote the ServerSideAdd function like you would any other JavaScript function.  You can call it using the two parameters, or optionally pass a call back function.  The name of the function is, by default, <name of class>.<name of server side function>, such as Index.ServerSideAdd:

alertIndex.ServerSideAdd(100,99));

OR

Index.ServerSideAdd(100,99, ServerSideAdd_CallBack);

function ServerSideAdd_CallBack(response){
 alert(response.value);
}

The response exposes three properties, error, value and request.

Note that you can return more complex objects that simple types.

See the demo for additional information:
http://ajax.schwarz-interactive.de/csharpsample/default.aspx

==========================================================

這是下載AJAX后里面的一個說明檔案。簡單粗略的翻譯一下(沒有按照原文翻譯):
添加一個AJAX的方法操作如下:
第一步:在項目裡添加對Ajax.DLL的引用。
第二步:修改Web.config檔案:主要是添加一個Handle的引用。
<configuration>
  <system.web>
    <httpHandlers>
    <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
    </httpHandlers> 
    ...
  <system.web>
</configuration>
第三步:在頁面載入時添加添下面的函數:
//C#
public class Index : System.Web.UI.Page{
   private void Page_Load(object sender, EventArgs e){
      Ajax.Utility.RegisterTypeForAjax(typeof(Index));     
      //... 
   }
   //... 
}
註:感覺這裡“曲解”了WebService!特別是後面的函數。
第四步:在頁面裡添加Ajax的函數,讓它具有屬性:Ajax.JavascriptMethodAttribute():
//C#
[Ajax.AjaxMethod()]
public int ServerSideAdd(int firstNumber, int secondNumber)
{
return firstNumber + secondNumber;
}
註:這裡的函數是在背景頁面裡添加,而不是獨立的WebService!本質上講,WebService與普通的頁面一樣,都是基於Http協議的,因此,一般頁面也好,WebService的asmx頁面也好,沒有本質的區別,只是在給不同的程式引用的時候,一些協議不一樣。但我個人覺得,Ajax的這做做法不是很可取,它把服務函數與頁面函數都放在一起,管理上就會存在一些問題。而且,服務函數可能要給其它程式調用,而這裡只能給自己頁面裡的JS來調用。
第五步:JS調用:

alert(Index.ServerSideAdd(100,99).value);//原文有誤,原文為:alertIndex.ServerSideAdd(100,99));

OR

Index.ServerSideAdd(100,99, ServerSideAdd_CallBack);

function ServerSideAdd_CallBack(response){
 alert(response.value);
}

OK,完成了!

然而我卻有以下幾個問題不明白:
1、AJAX的函數是在頁面裡的,這樣會不會使頁面邏輯變的混亂,搞不清楚哪是服務函數,哪是頁面函數。
2、JS的調用也只是在本頁裡,如果我們要跨頁面調用函數,或者服務呢?(後來知道了,有類的引用,可以跨頁面調用。)
3、可以直接調用已經存在的服務函數嗎?如果項目裡已經存在了大量的服務函數,難道我還一個一個的都改成頁面函數嗎?還要添加Ajax屬性,我覺得不可取!最鬱悶的是在頁面轉入時的Ajax註冊,簡直無法接受。想想吧,那樣的話,你要改多少內容?

因此,我覺得我們所面臨的問題,如果用JS直接調用WebService上的函數!而不是像Ajax那樣,將頁面函數修改後用JS來調用!對現有WebService的Ajax改造。

1、在使用者控制項或者頁面裡註冊AJAX:
   Ajax.Utility.RegisterTypeForAjax(typeof(Webb.WAVE.WebService.Service4FolderAndFiles)); 
你可以註冊任何的一個類,而並不局限於Page類,當然,你的函數也就可以寫在Web Service裡了。
2、修改原服務函數,添加Ajax屬性:
  [Ajax.AjaxMethod()]
  [WebMethod]
  public int ServerSideAdd(int firstNumber, int secondNumber)
  {
   return firstNumber + secondNumber;
  }
即,你完全可以給一個函數兩個屬性,這樣你的代碼就不用修改的太多了。
3、JS調用,與上面的就沒什麼兩樣了。

總結:從上面的例子上看的出來,Ajax已經不在是局限於服務函數的調用,它將頁面的後台函數前台化,可以在JS裡方便的調用它們。當然,原有的Web Service函數也是都可以不改變的,這就為我們的項目改造提供的很好的方便,你願意改就改,添加一個Ajax方法屬性就行了。不想,重新再寫也行。當然,裡主要的還是原來的函數可以保證你的原有項目都能很好的運行。

最後提一下的就是註冊Ajax類時候,Ajax.Utility.RegisterTypeForAjax(typeof(Webb.WAVE.WebService.Service4FolderAndFiles)); 
它向頁面裡添加了一些JS代碼。正是這些代碼請求了伺服器上的函數,而伺服器再通過Handle來處理不同的請求來定位到伺服器上的函數。

相關文章

聯繫我們

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