ドキュメント

Tutorials:Using AjaxPro

From jQuery JavaScript Library

Jump to: navigation, search

An example on how to use jQuery and Ajax.NET Professional together.

Original: http://weblogs.asp.net/mschwarz/archive/2007/04/10/jquery-and-ajax-net-professional-ajaxpro.aspx
Author: Michael Schwarz

Similar Tutorials: Ajax

Get Started with the AjaxPro code

Ajax.NET Professional (AjaxPro) is a Ajax Framework for the Microsoft .NET Framework which you can use with any .NET language like C#, VB.NET, J# to create JavaScript proxies to invoke any .NET method from the client-side. Download the AjaxPro.2.dll (for .NET 2.0) from http://www.ajaxpro.info/, add the reference and setup the web.config:

 <configuration>
   <location path="ajaxpro">
     <system.web>
       <httpHandlers>
         <add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
       </httpHandlers>
     </system.web>
   </location>
 </configuration>

Next we need to write our .NET method that will return the data we will ask for. A simple static method can be used with nearly any .NET data type, but I recommend to use only structs/classes with JavaScript similar data types for the properties.

 namespace MyNamespace
 {
   public class MyClass
   {
     [AjaxPro.AjaxMethod]
     public static string HelloWorld(string s)
     {
       return "Hello " + s;
     }
   }
 }

The Ajax endpoint

To get the name of the Ajax request endpoint you must use the namespace, classname and the assembly name like below:

 string path = "/ajaxpro/MyNamespace.MyClass,";
 path += typeof (MyClass).Assembly.FullName.Split(',')[0];
 path += ".ashx";

Write the above string to a JavaScript variable using RegisterHiddenField or any other way.

Run the AjaxPro request

The next step would be to use jQuery to invoke the AjaxPro request. I used a very simple example to show what you need.

 $.ajax({
   type: "POST",
   url: MyAjaxPath,     // this is the path from above
   data: '{"s":"Hans"}',
   beforeSend: function(xhr) {
     xhr.setRequestHeader("X-AjaxPro-Method", "HelloWorld");
   },
   success: function(s) {
     var o = null;
     eval("o = " + s + "*"+"/");
     alert(o);
   }
 });