The ajax. js in Prototype provides a very useful ajax framework. Generally, you can simply call the following code in an application.
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:
Only oneGetTransportMethod. An XMLHttpRequest object is returned.ActiveRequestCountAttribute, reflecting the number of ajax currently being processed
Ajax. Responders object:
Inherits from Enumerable to manage global Ajax requests.
Register (responder ):Register an object for managing ajax requests
Unregister (responder ):Revoking an object that manages ajax requests
Dispatch (callback, request, transport, json ):How to trigger registration to process 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 this parameter 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 ):Send a request, which is called when new is used. Therefore, you do not need to use
Success ():Determine whether the request is successful
GetHeader (name ):Obtain 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.
Start ():Start update. It is automatically called during initialization.
Stop ():Stop update