Like many developers who used this class library before, at the beginning, I had to read prototype. js Source code And experiment with its functions. The Ajax. js in prototype provides a very useful Ajax framework. Code You can.
New Ajax. Request (
URL, {method: "Get ",
Onsuccess: showfilter,
Onfailure: function (request) {alert ("Server Error !")},
Onexception: showerror}
);
This framework provides the following objects and methods:
Ajax object: there is only one gettransport method. One XMLHTTPRequest object is returned, and the other is the activerequestcount attribute, reflecting the number of Ajax currently being processed.
Ajax. Responders object: inherits from enumerable to manage global Ajax requests. The method is as follows:
Register (responder): registers an object that manages Ajax requests.
Unregister (responder): cancels an object that manages Ajax requests.
Dispatch (callback, request, transport, JSON): Method for triggering registered processing objects
This object is rarely used. The following code has been used in the system to register a processing object.
Ajax. Responders. Register ({
Oncreate: function (){
Ajax. activerequestcount ++;
},
Oncomplete: function (){
Ajax. activerequestcount -;
}
});
Ajax. base class:
The base class of Ajax has only one method setoptions (options). The default request parameter is as follows. You can specify it when creating Ajax. Request:
Method: 'post ',
Asynchronous: True,
Contenttype: 'application/X-WWW-form-urlencoded ',
Encoding: 'utf-8 ′,
Ajax. Request class:
The main Ajax class is inherited from Ajax. base class. The client uses new Ajax. request (URL, options) Call. options is an object (associated array). In options, you can specify options such as method, asynchronous, contenttype, encoding, parameters, postbody, username, and password, parameters can be a character or an array object,
You can also specify request heads through requestheaders in options. The requestheaders can be an array (for example, ["connection", "close", "aheadkey", "aheadvalue"]). or an associated array;
The most important option in options is to specify the Ajax callback method. oncomplete, onsuccess, onfailure, and onexception can be defined (methods with abnormal calls during execution, mainly oncomplete, onsuccess, onfailure and other callback methods), you can even define callback methods such as on404 and on503. Their parameters are (transport, JSON), where transport is the request's XMLHTTPRequest object, JSON is the result of evaljson
If a Javascript file is returned (determined based on the returned Content-Type header), The evalresponse method is executed. In addition, the Ajax. Request object also has an evaljson method, which is executed when the file is obtained.
The method list of this object is as follows:
Request (URL): The request is called when new is sent.
Success (): determines whether the request is successful
Getheader (name): Get the request head according to the name
Evaljson (): Execute getheader ("X-JSON") and return the result
Evalresponse (): Execute the returned responsetext and return
Ajax. Updater class:
Inherited from Ajax. request, just better than Ajax. request adds the HTML element update function. The general method is new Ajax. updater (element, URL, options) and element can be an element or {success: E1, failure: E2,
By default, Javascript in the returned results will not be executed. If you execute it first, you can specify evalscripts in options to true.
By default, the content of the specified element is replaced. If you want to add the content, you can specify the insertion parameter of options. insertion is an insertion. before, insertion. top or insertion. bottom, insertion. after (will be in the Dom. JS)
Ajax. periodicalupdater class:
Inherited from Ajax. base, which periodically updates the content of an HTML element. This class calls Ajax. updater periodically updates HTML elements by using the new Ajax method. periodicalupdater (container, URL, options), parameters and Ajax. updater is similar. options can be set to frequency (2 by default). Decay and decay indicate the multiples of frequency to be extended when the request content does not change. The default value is 1, for example, if decay is set to 2, frequency is set to 3, and the content remains unchanged, the request time will change to 3, 6, 12, 24, and so on.
Http://www.hack4.com/article/hack4-12251.html? Jdfwkey = j1qpb