AJAX using Prototype Library - I

Friday 6 May 2011 Posted by Sridharan Natarajan
Prototype is a most commonly used javascript library. Prototype provides a number of utility methods to access DOM and AJAX.

As we saw in the previous post, we have to create a XMLHTTPRequest for each browser, send the request and have to define a callback method to receive and process the response. AJAX made simple with Prototype.

Prototype library provides various functions for AJAX for our convenience.

Below are the list for the common options for request and callbacks used with Prototype.

Common options

Option Default Description
asynchronous true Determines whether XMLHttpRequest
is used asynchronously or not. Since synchronous usage is rather unsettling, and
usually bad taste, you should avoid changing this. Seriously.
contentType 'application/ x-www-form-urlencoded' The Content-Type header for your request. You might want to send XML in-stead of the regular URL-encoded format, in which case you would have to change this.
encoding 'UTF-8' The encoding for your request contents. It is best left as is, but should weird encoding issues arise, you may have to tweak it in accordance with other encoding related parts of your page code and server side.
method 'post' The HTTP method to use for the request. The other widespread possibility is 'get'. As a Ruby On Rails special, Prototype also reacts to other verbs (such as 'put' and 'delete' by actually using 'post' and putting an extra '_method' parameter with the originally requested method in there.
parameters ' ' The parameters for the request, which will be encoded into the URL for a 'get' method, or into the request body for the other methods. This can be provided either as a URL-encoded string
or as any Hash-compatible object
(basically anything), with properties representing parameters.
postBody None Specific contents for the request body on a 'post' method (actual method, after possible conversion as described in the
method opt ion above). If it is not provided, the contents of the parameters option will be used instead.
requestHeaders See text Request headers can be passed under
two forms:
  • As an object, with properties representing headers.
  • As an array, with even-index (0, 2...) elements being header names, and odd-index (1, 3...) elements being values.

Prototype automatically provides a set of default headers, that this option can override and augment:
  • X-Requested-With is set to 'XMLHttpRequest'.
  • X-Prototype-Version
    provides Prototype's current version (e.g. 1.5.0).
  • Accept defaults to 'text/ javascript, text/html,
    application/xml, text/ xml, */*'
  • Content-type is built based on the contentType and encoding options.

Common Callbacks

Callback Description
onComplete Triggered at the very end of a request's life-cycle, once the request completed,
status-specific callbacks were called, and possible automatic behaviors were processed.
onException Triggered whenever an XHR error arises. Has a custom signature: the first argument is the requester (i.e. an Ajax.Request instance), the second is the exception object.
onFailure Invoked when a request completes and its status code exists but is not in the 2xy
family. This is skipped if a code-specific callback is defined, and happens before onComplete.
onInteractive (Not guaranteed) Triggered whenever the requester receives a part of the response
(but not the final part), should it be sent in several packets.
onLoaded (Not guaranteed) Triggered once the underlying XHR object is setup, the connection open, and ready to send its actual request.
onLoading (Not guaranteed) Triggered when the underlying XHR object is being setup, and its connection opened.
onSuccess Invoked when a request completes and its status code is undefined or belongs in
the 2xy family. This is skipped if a code-specific callback is defined, and happens before onComplete.
onUninitialized (Not guaranteed) Invoked when the XHR object was just created.
onXYZ With XYZ being an HTTP status code for the response. Invoked when the response just completed, and the status code is exactly the one we used in t he callback name. Prevents execution of onSuccess / onFailure. Happens before onComplete.
 

Responder Callbacks

Callback Description
onCreate Triggered whenever a requester object from the Ajax namespace is created, after
its parameters where adjusted and its before its XHR connection is opened. This
takes two arguments: the requester object and the underlying XHR object.
onComplete Triggered at the very end of a request's life-cycle, once the request completed,
status-specific callbacks were called, and possible automatic behaviors were processed.
onException Triggered whenever an XHR error arises. Has a custom signature: the first argument is the requester (i.e. an Ajax.Request instance), the second is the exception object.
onInteractive (Not guaranteed) Triggered whenever the requester receives a part of the response
(but not the final part), should it be sent in several packets.
onLoaded (Not guaranteed) Triggered once the underlying XHR object is setup, the connection open, and ready to send its actual request.
onLoading (Not guaranteed) Triggered when the underlying XHR object is being setup, and its connection opened.
onUninitialized (Not guaranteed) Invoked when the XHR object was just created.
Labels:

Post a Comment