Home > Web Front-end > JS Tutorial > Prototype usage guide ajax_prototype

Prototype usage guide ajax_prototype

WBOY
Release: 2016-05-16 19:21:14
Original
1158 people have browsed it

ajax.js in Prototype provides a very easy-to-use ajax framework. In general applications, simply call the following code

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, which returns an XMLHttpRequest object, and there is another activeRequestCount attribute, which reflects the number of ajax currently being processed

Ajax.Responders object:

Inherits from Enumerable, manages global Ajax requests, and has the following methods
register(responder): Register an object that manages ajax requests
unregister(responder): Revoke an object that manages ajax requests
dispatch(callback, request, transport, json): Trigger the registered processing object method

This object is rarely used. A processing object has been registered in the system using the following code

Ajax.Responders.register({
onCreate: function() {
Ajax.activeRequestCount ;
},
onComplete: function() {
Ajax.activeRequestCount– ;
}
});

Ajax.Base class:

Ajax base class has only one method setOptions(options). The default request parameters are as follows. You can specify them when creating a new Ajax.request:

method: 'post',
asynchronous: true,
contentType: 'application/x-www-form-urlencoded',
encoding: 'UTF-8′,

Ajax.Request class:

The main class of ajax inherits from the ajax.base class. The client uses new Ajax.Request(url, options) to call. options is an object (associative array). In options, you can specify method, asynchronous, contentType, and encoding. , parameters, postBody, username, password and other options, where parameters can be character strings or associative arrays,

In addition, request heads can also be specified through requestHeaders in options, where requestHeaders can be an array (such as ["Connection", "Close", "aheadkey", "aheadvalue"]) or an associative array;

The most important option in options is to specify the callback method of ajax. You can define onComplete, onSuccess, onFailure, onException (methods called abnormally during execution, mainly generated by onComplete, onSuccess, onFailure and other callback methods), or even You can define callback methods such as on404 and on503. Their parameters are (transport, json), where transport is the requested XMLHttpRequest object and json is the result of evalJSON

If a javascript file is returned (based on the returned Content-type header), the evalResponse method will be executed. In addition, the Ajax.Request 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) : When sending a request, it has already been called when new, so there is generally no need to use
success(): Determine whether the request is successful
getHeader(name): Get the request head according to the name
evalJSON(): Execute getHeader("X-JSON"), and returns the result
evalResponse(): executes the returned responseText and returns

Ajax.Updater class:

Inherits from Ajax.Request, but adds the function of updating html elements than Ajax.Request. The general usage method is new Ajax.Updater(element, url, options), element can be an element, or {success: e1,failure:e2}This form,

By default, the javascript in the returned result will not be executed. If you execute it first, you can specify evalScripts in options to be true

By default, the content of the specified element is replaced. If you want to add it, you can specify the insertion parameter of options. insertion is an Insertion.Before, Insertion.Top or Insertion.Bottom, Insertion.After (will be in dom.js (introduced)

Ajax.PeriodicalUpdater class:

Inherited from Ajax.Base, it periodically updates the content of an html element. This class will call Ajax.Updater to periodically update the html element. The usage method is new Ajax.PeriodicalUpdater(container, url, options), The parameters are similar to Ajax.Updater. Options can set frequency (default is 2) and decay. Decay refers to the multiple that the frequency needs to be extended when the requested content does not change. The default is 1. For example, if decay is set to 2, If the frequency is set to 3 and the content has not changed, the requested time will change to 3, 6, 12, 24, etc.

start(): Start updating. It will be automatically called during initialization.
stop(): Stop updating

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template