This blog shows the data exchange process in ASP. NET Ajax.
Employee class:
/// <Summary> /// employee class /// </Summary> public class employee {private string _ firstname; private string _ lastname; private string _ title; public Employee () {}/// <summary> /// constructor with parameters /// </Summary> /// <Param name = "firstname"> last name </param> // /<Param name = "lastname"> name </param> // <Param name = "title"> position </param> public employee (string firstname, string lastname, String title) {This. _ firstname = firstname; this. _ lastname = lastname; this. _ Title = title ;}/// <summary> /// surname /// </Summary> Public String firstname {get {return this. _ firstname ;}/// <summary> /// name // </Summary> Public String lastname {get {return this. _ lastname ;}/// <summary> // position /// </Summary> Public String title {get {return this. _ title ;}}}
Client code
Getemployee. ashx
<% @ Webhandler Language = "C #" class = "aspnetajaxoverview. getemployee "%> using system; using system. web; using system. web. script. serialization; namespace aspnetajaxoverview {public class getemployee: ihttphandler {public void processrequest (httpcontext context) {context. response. contenttype = "text/plain"; // obtain the last name, name, and position string firstname = context. request. params ["firstname"]; string lastname = context. request. params ["lastname"]; String title = context. request. params ["title"]; // instance employee class employee Employee = new employee (firstname, lastname, title); // serialized as a JSON string javascriptserializer serializer = new javascriptserializer (); string jsonemp = serializer. serialize (employee); // Response Request context. response. write (jsonemp);} public bool isreusable {get {return false ;}}}}
Server code
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "2 asynchronous communication. aspx. cs" inherits = "_ 2 asynchronous communication" %> <! 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">
Running result
The data exchange process adopts the sequence diagram for visual display:
The javascript in the middle is equivalent to an asynchronous communication layer, which is responsible for data exchange between browser and server.