Jquery can call several methods to implement Ajax
I often use the $ get (URL, Data, callback, type) method.
The URL is the page of the asynchronous request (which can be. ashx file), data is the parameter, callback is the callback function, and type is the type of the returned data. types include XML, HTML, JSON, and text.
First, the page references jquery. js
Write JS functions processed by Ajax on the page
Function initmeeting () {$. get ("/common/meetingroom. ashx ", {meetid: <% = meetid % >}, function sellerlist (data) {$ (" # divsellerlist ").html (data) ;}," JSON "); setTimeout ("initmeeting ()", 20000);} function initmeeting () {$. get ("/common/meetingroom. ashx ", {meetid: <% = meetid % >}, function sellerlist (data) {var OBJ = eval (" ("+ Data + ")"); // converted JSON object $ ("# divsellerlist" ).html (obj. cellerlist) ;}, "html"); setTimeout ("initmeeting ()", 20000 );}
The returned type is JSON, which can return data types of similar classes. For example, {"name": "Sunny D. D", "Age": 25}
However, when using the returned data value, you must first convert the JSON
VaR OBJ = eval ("(" + Data + ")"); // converted JSON object
You can get the JSON object.
The JSON object is generated in the meetingroom. ashx file.
Some code is as follows:
Public class meetingroom: ihttphandler {public void processrequest (httpcontext context) {context. response. contenttype = "text/plain"; try {int meetid = xyconvert. getint32 (context. request. querystring ["meetid"]); string STR = ""; meetingjson = new meetingjson (); If (meetid! = 0) {meetingjson. cellerlist = returncellerlist (meetid);} STR = jsonconvert. serializeobject (meetingjson); context. response. write (STR);} catch (exception ex) {context. response. write (ex. message) ;}} public class meetingjson {string cellerlist; Public String cellerlist {get {return cellerlist;} set {cellerlist = value ;}} protected string returncellerlist (INT meetid) {// omitted ..}}
The most important thing to return the JSON data format is to use the jsonconvert. serializeobject () method. It can package the data to be passed to the client and serialize it as a string.
The class jsonconvert can be used by introducing DLL in a third-party DLL file (newtonsoft. JSON. dll.
Note: In. in the ashx page, if you want to use the session, write the context directly. session ["user"] does not work. You must specify the current context to use the session. You can use the irequiressessionstate interface to access the session.
public class MeetingRoom : IHttpHandler, IRequiresSessionState {
In small and medium-sized projects, Ajax is implemented in this way. For large projects, there should be a better encapsulated Ajax framework.