Prototype provides a very useful ajax framework. Generally, simply call the following code in applications.
new (
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, return an XMLHttpRequest object, and anotheractiveRequestCountAttribute, reflects the number of ajax currently being processed
Object:
Inherited from Enumerable, manage global Ajax requests, with the following methods
register(responder):Register an object to manage ajax requests
unregister(responder):Revoke an object that manages ajax requests
dispatch(callback, request, transport, json):How to trigger the registered processing object
This object is generally rarely used, and the system has already registered a processing object using the following code
({
onCreate: function() {
++;
},
onComplete: function() {
–;
}
});
kind:
Ajax's base class, only one methodsetOptions(options), The default request parameters are as follows, you can specify when creating:
method: ‘post',
asynchronous: true,
contentType: ‘application/x-www-form-urlencoded',
encoding: ‘UTF-8′,
kind:
The main class of ajax is inherited from the class. The client uses new (url, options) to call it. options is an object (association array). In options, you can specify method, asynchronous, contentType, encoding, parameters, postBody, username, password and other options. These parameters can be character passes or associative array objects.
In addition, in options, request heads can also be specified through requestHeaders, where requestHeaders can be an array (for example ["Connection", "Close", "aheadkey", "aheadvalue"]) or an associative array;
The most important option in options is to specify ajax callback method, which can define onComplete, onSuccess, onFailure, onException (the method that occurs during execution is mainly generated by onComplete, onSuccess, onFailure and other callback methods). It can even define callback methods such as on404 and on503. Their parameters are (transport, json), where transport is the requested XMLHttpRequest object, json is the result of evalJSON.
If the returned javascript file (judged according to the returned Content-type header), the evalResponse method will be executed, and the object also has an evalJSON method, which will be executed when the file is obtained.
The method list of this object is as follows:
request(url) :Send a request, it is already called when new, so it is generally not necessary to use it.
success():Determine whether the request has been successful
getHeader(name):Get request head according to name
evalJSON():Execute getHeader("X-JSON") and return the result
evalResponse():Execute the returned responseText and return
kind:
Inherited from, it only adds the function of updating html elements. The general usage method is new (element, url, options). element can be an element or {success:e1, failure:e2}.
By default, the JavaScript in the returned result will not be executed. If you execute first, you can specify that the evalScripts in options are true.
By default, replace the content of the specified element. If you want to add, you can specify the insertion parameter of options. Insertion is a, or, (which will be introduced in)
kind:
Inherited from, periodically update the content of an html element. This class will call to periodically update the html element. The usage method is new (container, url, options), and the parameters are similar. Among them, options can be set to frequency (default is 2), decay, decay refers to the multiple that frequency needs to be extended when the requested content has not changed. The default is 1. For example, if decay is set to 2 and frequency is set to 3 and the content has not changed, the request time will become 3, 6, 12, 24, etc.
start():It will be automatically called when initializing
stop():Stop update