Ajax|ajax Framework
Introduction to Ajax
Ajax is not a new technology, but an organic combination of some existing technologies, mainly including: XmlHttp, reflect. An AJAX framework basically includes a custom HttpHandler, a section of JavaScript code.
Ajax operating mechanism:
Before we use XMLHTTP to achieve no Refresh page, is to use XMLHTTP to request a hidden page, using (asp/asp.net) from the HttpHandler, and in Ajax, we are asking for a hidden page, The difference is that the HttpHandler of this page is implemented by ourselves. To create your own Ajax:
1. First we implement an HTTP handler (HttpHandler) to respond to the client's request:
Implementing a custom HttpHandler requires implementing the IHttpHandler interface.
The interface contains a property and a method:
BOOL Ihttphandler.isreusable void Ihttphandler.processrequest (HttpContext context) Example: BOOL Ihttphandler.isreusable { get {return true;} } void Ihttphandler.processrequest (HttpContext context) { Context. Response.Clear (); Gets the method to invoke String methodname = Context. Request.querystring["Me"]; Gets the assembly information. Czhenq.AJAX.Class1.Dencode is a custom string encoding method String assemblyname = Czhenq.AJAX.Class1.Dencode (context. Request.querystring["as"]); Gets the parameters of the method String Arguments = context. request.querystring["AR"]; Start Calling method Type type = Type.GetType (AssemblyName); MethodInfo method = Type. GetMethod (methodname, BindingFlags.NonPublic | BindingFlags.Public | bindingflags.static | BindingFlags.Instance); if (method!= null) { Parameter uses the "," delimited string[] args = Arguments.split (",". ToCharArray ()); Parameterinfo[] Paras = method. GetParameters (); object[] argument = new Object[paras. Length]; for (int i = 0; i < argument. Length; i++) { if (i < args. Length) { The conversion must be made because the parameters passed by XMLHTTP are all string types. Only the parameters are converted to Int32, and there is no other consideration. Argument[i] = Convert.ToInt32 (Args[i]); } } Object value = method. Invoke (Activator.CreateInstance (type, true), argument); if (value!= null) context. Response.Write (value. ToString ()); else context. Response.Write ("error"); } End of process Context. Response.End (); } |
2. Client JavaScript code:
function Callmethod (Assemblyname,methodname,argus) { var args = ""; for (Var i=0;i<> args + = Argus[i] + ","; if (args.length>0) args = Args.substr (0,args.length-1); var xmlhttp = new ActiveXObject (' microsoft.xmlhttp '); url = "ajax/ajax.czhenq?as=" + AssemblyName + "&me=" + methodname + "&ar=" + args; Xmlhttp.open ("POST", Url,false); Xmlhttp.send (); alert (Xmlhttp.responsetext); } |
3. A simple AJAX framework has been implemented. Now write a piece of code to test.
Use your own Ajax:
1. Create a new website and apply the HttpHandler you just wrote. and register your HttpHandler in the web.config of the website, stating that those requests will be processed using the handler you write. The following description: All requests ending with "CZQ" will be processed using "czhenq.httphandlerfactory".
2. Add a Web page, copy just the script to the page, and add a method you want to call.
private string Add (int i, int j) { return TextBox1.Text; } |
3. Place a HiddenField control on the page named AssemblyName. and add the following code to the Page_Load:
String assemblyname = Czhenq.AJAX.Class1.Encode (typeof (_default). AssemblyQualifiedName); Assemblyname.value = AssemblyName; |
4. Add the following script to the page:
var assemblyname = document.getElementById ("AssemblyName"); var argus = new Array (); Argus.push ("100"); Argus.push ("200"); Callmethod (AssemblyName, "Add", Argus); |
Summary:
Ajax is not a new technology, it's just an organic combination of some existing technologies. We can simply interpret Ajax as: Ajax is the encapsulation of invoking XMLHTTP on JavaScript. It changes the way code is written.
attachedEncode and Dencode implementation:
public static string Encode (string value) { byte[] bytes = ASCIIEncoding.ASCII.GetBytes (value); Return convert.tobase64string (bytes); } public static string Dencode (string value) { byte[] bytes = convert.frombase64string (value); Return ASCIIEncoding.ASCII.GetString (bytes); } |
- Ajax: A new way to build Web apps
- Discussion on the error handling mechanism of AJAX (2)
- Discussion on the error handling mechanism of AJAX (1)
- First experience. NET Ajax Brushless New technology
- A brief analysis of Ajax development Technology in Rails system (4)