<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Prototype JavaScript framework - Ajax</title>
  <id>tag:prototypejs.org.,2008:mephisto/api/ajax</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://prototypejs.org./feed/api/ajax/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://prototypejs.org./api/ajax" rel="alternate" type="text/html"/>
  <updated>2008-01-16T11:04:04Z</updated>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Tobie</name>
    </author>
    <id>tag:prototypejs.org.,2007-12-24:18612</id>
    <published>2007-12-24T00:41:00Z</published>
    <updated>2008-01-16T11:04:04Z</updated>
    <category term="Ajax"/>
    <category term="1.6"/>
    <link href="http://prototypejs.org./api/ajax/response" rel="alternate" type="text/html"/>
    <title>Ajax.Response</title>
<summary type="html">&lt;p&gt;The object passed as the first argument of all Ajax requests callbacks.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;The object passed as the first argument of all Ajax requests callbacks.&lt;/p&gt;
&lt;p&gt;This is a wrapper around the native &lt;code&gt;xmlHttpRequest&lt;/code&gt; object (or it&#8217;s &lt;code&gt;ActiveX&lt;/code&gt; counterpart). It normalizes cross-browser issues while adding support for JSON via the &lt;code&gt;responseJSON&lt;/code&gt; and &lt;code&gt;headerJSON&lt;/code&gt; properties. &lt;/p&gt;

&lt;h3&gt;Properties of the &lt;code&gt;Ajax.Response&lt;/code&gt; object&lt;/h3&gt;

&lt;table class=&quot;data&quot;&gt;
&lt;thead&gt;
  &lt;tr&gt;
    &lt;th&gt;Property&lt;/th&gt;
    &lt;th&gt;Type&lt;/th&gt;
    &lt;th&gt;Description&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;status&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;Number&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The HTTP status code sent by the server.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;statusText&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;String&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The HTTP status text sent by the server.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt; readyState &lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;Number&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The request&#8217;s current state. &lt;code&gt;0&lt;/code&gt; corresponds to &lt;code&gt;&quot;Uninitialized&quot;&lt;/code&gt;, &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;&quot;Loading&quot;&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt; to &lt;code&gt;&quot;Loaded&quot;&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt; to &lt;code&gt;&quot;Interactive&quot;&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt; to &lt;code&gt;&quot;Complete&quot;&lt;/code&gt;.&lt;/td&gt;
  &lt;/tr&gt;

  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;responseText&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;String&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The text body of the response.&lt;/td&gt;
  &lt;/tr&gt;

  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;responseXML&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;document Object&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The XML body of the response if the content-type of the request is set to &lt;code&gt;application/xml&lt;/code&gt;. &lt;code&gt;null&lt;/code&gt; otherwise.&lt;/td&gt;
  &lt;/tr&gt;

  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;responseJSON&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;Object&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The JSON body of the response if the content-type of the request is set to &lt;code&gt;application/json&lt;/code&gt;. &lt;code&gt;null&lt;/code&gt; otherwise.&lt;/td&gt;
  &lt;/tr&gt;

  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;headerJSON&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;Object&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;Auto-evaluated content of the &lt;code&gt;X-JSON&lt;/code&gt; header if present. &lt;code&gt;null&lt;/code&gt; otherwise. This is useful to transfer &lt;em&gt;small&lt;/em&gt; amounts of data.&lt;/td&gt;
  &lt;/tr&gt;

  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;request&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;Object&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The request object itself (an instance of &lt;code&gt;Ajax.Request&lt;/code&gt; or &lt;code&gt;Ajax.Updater&lt;/code&gt;).&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;transport&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;Object&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The native &lt;code&gt;xmlHttpRequest&lt;/code&gt; object itself.&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;h3&gt;Methods of the &lt;code&gt;Ajax.Response&lt;/code&gt; object&lt;/h3&gt;

&lt;table class=&quot;data&quot;&gt;
&lt;thead&gt;
  &lt;tr&gt;
    &lt;th&gt;Method&lt;/th&gt;
    &lt;th&gt;Type&lt;/th&gt;
    &lt;th&gt;Description&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;getHeader(name)&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;String&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;Returns the value of the requested header if present. &lt;code&gt;null&lt;/code&gt; otherwise. Does &lt;em&gt;not&lt;/em&gt; throw errors on undefined headers like it&#8217;s native counterpart does.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
     &lt;td&gt;&lt;code&gt;getAllHeaders()&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;String&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;Returns a string containing all headers separated by a line break. Does &lt;em&gt;not&lt;/em&gt; throw errors if no headers are present like it&#8217;s native counterpart does.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;code&gt;getResponseHeader(name)&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;String&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;Returns the value of the requested header if present. Throws an error otherwise. This is just a wrapper around the &lt;code&gt;xmlHttpRequest&lt;/code&gt; object&#8217;s native method. Prefer it&#8217;s shorter counterpart &lt;code&gt;getHeader&lt;/code&gt;.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
     &lt;td&gt;&lt;code&gt;getAllResponseHeaders()&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;&lt;code&gt;String&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;Returns a string containing all headers separated by a line break. Throws an error otherwise. This is just a wrapper around the &lt;code&gt;xmlHttpRequest&lt;/code&gt; object&#8217;s native method. Prefer it&#8217;s shorter counterpart &lt;code&gt;getAllHeaders&lt;/code&gt;.&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Christophe</name>
    </author>
    <id>tag:prototypejs.org.,2007-01-12:13043</id>
    <published>2007-01-12T22:06:00Z</published>
    <updated>2007-01-12T22:06:54Z</updated>
    <category term="Ajax"/>
    <link href="http://prototypejs.org./api/ajax/responders" rel="alternate" type="text/html"/>
    <title>Ajax.Responders</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Ajax.Responders.register(responder)
Ajax.Responders.unregister(responder)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A repository of global listeners notified about every step of Prototype-based AJAX requests.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;Ajax.Responders.register(responder)
Ajax.Responders.unregister(responder)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A repository of global listeners notified about every step of Prototype-based AJAX requests.&lt;/p&gt;
&lt;p&gt;Sometimes, you need to provide generic behaviors over all AJAX operations happening in the page (through &lt;a href=&quot;/api/ajax/request&quot;&gt;&lt;code&gt;Ajax.Request&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;/api/ajax/updater&quot;&gt;&lt;code&gt;Ajax.Updater&lt;/code&gt;&lt;/a&gt; or &lt;a href=&quot;/api/ajax/periodicalUpdater&quot;&gt;&lt;code&gt;Ajax.PeriodicalUpdater&lt;/code&gt;&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;For instance, you might want to automatically show an indicator when an AJAX request is ongoing, and hide it when none are.  You may well want to factor out exception handling as well, logging those somewhere on the page in a custom fashion.  The possibilities are plenty.&lt;/p&gt;

&lt;p&gt;To achieve this, Prototype provides &lt;code&gt;Ajax.Responders&lt;/code&gt;, which lets you register (and if you wish to, unregister later) &lt;strong&gt;responders&lt;/strong&gt;, which are objects with properly-named methods.  These names are the regular callback names, and your responders can implement any set of interest.&lt;/p&gt;

&lt;p&gt;For instance, Prototype automatically registers a responder that maintains a nifty variable: &lt;code&gt;Ajax.activeRequestCount&lt;/code&gt;.  This contains, at any time, the amount of currently active AJAX requests (those created by Prototype, anyway), by monitoring their &lt;code&gt;onCreate&lt;/code&gt; and &lt;code&gt;onComplete&lt;/code&gt; events.  The code for this is fairly simple:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
Ajax.Responders.register({
  onCreate: function() {
    Ajax.activeRequestCount++;
  },
  onComplete: function() {
    Ajax.activeRequestCount--;
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href=&quot;/api/ajax/options&quot;&gt;All callbacks&lt;/a&gt; in the life-cycle are available; actually, &lt;code&gt;onCreate&lt;/code&gt; is only available to responders, as it wouldn&#8217;t make a lot of sense to individual requests: you do know when your code creates them, don&#8217;t you?  It is triggered even before the XHR connection is opened, which makes it happen right before &lt;code&gt;onUninitialized&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Unregister: remember the reference&#8230;&lt;/h3&gt;

&lt;p&gt;As always, unregistering something requires you to use the very same object you used at registration.  So if you plan on unregistering a responder, be sure to define it first, then pass the reference to &lt;code&gt;register&lt;/code&gt;, and finally, when the time comes, to &lt;code&gt;unregister&lt;/code&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Justin</name>
    </author>
    <id>tag:prototypejs.org.,2006-12-05:12742</id>
    <published>2006-12-05T21:42:00Z</published>
    <updated>2007-01-12T21:37:41Z</updated>
    <category term="Ajax"/>
    <link href="http://prototypejs.org./api/ajax" rel="alternate" type="text/html"/>
    <title>Ajax</title>
<content type="html">
            &lt;p&gt;Prototype offers three objects to deal with AJAX communication, which are listed below.  With Prototype, going Ajaxy is downright simple!  All three objects share a common set of options, which are discussed separately.&lt;/p&gt;

&lt;p&gt;The articles below provide you with several examples.  The Learn section also features a more narrative, &lt;a href=&quot;/learn/introduction-to-ajax&quot;&gt;tutorial-style article&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-11-18:12498</id>
    <published>2006-11-18T18:08:00Z</published>
    <updated>2008-01-25T23:32:05Z</updated>
    <category term="Ajax"/>
    <link href="http://prototypejs.org./api/ajax/options" rel="alternate" type="text/html"/>
    <title>Ajax Options</title>
<summary type="html">&lt;p&gt;This details all core options (shared by all AJAX requesters) and callbacks.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;This details all core options (shared by all AJAX requesters) and callbacks.&lt;/p&gt;
&lt;p&gt;All requester object in the &lt;code&gt;Ajax&lt;/code&gt; namespace share a common set of &lt;strong&gt;options&lt;/strong&gt; and &lt;strong&gt;callbacks&lt;/strong&gt;.  Callbacks are called at various points in the life-cycle of a request, and always feature the same list of arguments. They are passed to requesters right along with their other options.&lt;/p&gt;

&lt;h3&gt;Common options&lt;/h3&gt;

&lt;table class=&quot;data&quot;&gt;
&lt;thead&gt;
  &lt;tr&gt;
    &lt;th&gt;Option&lt;/th&gt;
    &lt;th&gt;Default&lt;/th&gt;
    &lt;th&gt;Description&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;asynchronous&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;Determines whether &lt;code&gt;XMLHttpRequest&lt;/code&gt; is used asynchronously or not.  Since synchronous usage is rather unsettling, and usually bad taste, you should avoid changing this.  Seriously.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;contentType&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;&lt;code&gt;'application/x-www-form-urlencoded'&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The &lt;code&gt;Content-Type&lt;/code&gt; header for your request.  You might want to send XML instead of the regular URL-encoded format, in which case you would have to change this.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;encoding&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;&lt;code&gt;'UTF-8'&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;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.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;method&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;&lt;code&gt;'post'&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The HTTP method to use for the request.  The other widespread possibility is &lt;code&gt;'get'&lt;/code&gt;.  As a Ruby On Rails special, Prototype also reacts to other verbs (such as &lt;code&gt;'put'&lt;/code&gt; and &lt;code&gt;'delete'&lt;/code&gt; by actually using &lt;code&gt;'post'&lt;/code&gt; and putting an extra &lt;code&gt;'_method'&lt;/code&gt; parameter with the originally requested method in there.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;parameters&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;&lt;code&gt;''&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;The parameters for the request, which will be encoded into the URL for a &lt;code&gt;'get'&lt;/code&gt; method, or into the request body for the other methods.  This can be provided either as a URL-encoded string or as any &lt;a href=&quot;/api/hash&quot;&gt;&lt;code&gt;Hash&lt;/code&gt;&lt;/a&gt;-compatible object (basically anything), with properties representing parameters.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;postBody&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;None&lt;/td&gt;
    &lt;td&gt;Specific contents for the request body on a &lt;code&gt;'post'&lt;/code&gt; method (actual method, after possible conversion as described in the &lt;code&gt;method&lt;/code&gt; opt
ion above).  If it is not provided, the contents of the &lt;code&gt;parameters&lt;/code&gt; option will be used instead.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;requestHeaders&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;See text&lt;/td&gt;
    &lt;td&gt;
      &lt;p&gt;Request headers can be passed under two forms:&lt;/p&gt;

      &lt;ul&gt;
        &lt;li&gt;As an &lt;strong&gt;object&lt;/strong&gt;, with properties representing headers.&lt;/li&gt;
        &lt;li&gt;As an &lt;strong&gt;array&lt;/strong&gt;, with even-index (0, 2&#8230;) elements being header names, and odd-index (1, 3&#8230;) elements being values.&lt;/li&gt;
      &lt;/ul&gt;

      &lt;p&gt;Prototype automatically provides a set of default headers, that this option can override and augment:&lt;/p&gt;

      &lt;ul&gt;
        &lt;li&gt;&lt;strong&gt;&lt;code&gt;X-Requested-With&lt;/code&gt;&lt;/strong&gt; is set to &lt;code&gt;'XMLHttpRequest'&lt;/code&gt;.&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;&lt;code&gt;X-Prototype-Version&lt;/code&gt;&lt;/strong&gt; provides Prototype's current version (e.g. 1.5.0).&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;&lt;code&gt;Accept&lt;/code&gt;&lt;/strong&gt; defaults to &lt;code&gt;'text/javascript, text/html, application/xml, text/xml, */*'&lt;/code&gt;&lt;/li&gt;
        &lt;li&gt;&lt;strong&gt;&lt;code&gt;Content-type&lt;/code&gt;&lt;/strong&gt; is built based on the &lt;code&gt;contentType&lt;/code&gt; and &lt;code&gt;encoding&lt;/code&gt; options.&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;evalJS&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;true&lt;/td&gt;
    &lt;td&gt;Automatically evals the content of &lt;code&gt;Ajax.Response#responseText&lt;/code&gt; if the content-type returned by the server is one of the following: &lt;code&gt;application/ecmascript&lt;/code&gt;,
&lt;code&gt;application/javascript&lt;/code&gt;,
&lt;code&gt;application/x-ecmascript&lt;/code&gt;,
&lt;code&gt;application/x-javascript&lt;/code&gt;,
&lt;code&gt;text/ecmascript&lt;/code&gt;,
&lt;code&gt;text/javascript&lt;/code&gt;,
&lt;code&gt;text/x-ecmascript&lt;/code&gt;, or 
&lt;code&gt;text/x-javascript&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; the request obeys &lt;a href=&quot;http://en.wikipedia.org/wiki/Same_origin_policy&quot;&gt;&lt;abbr title=&quot;Same Origin Policy&quot;&gt;SOP&lt;/abbr&gt;&lt;/a&gt;. If you need to force evalutation, pass &lt;code&gt;'force'&lt;/code&gt;. To prevent it altogether, pass &lt;code&gt;false&lt;/code&gt;.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;evalJSON&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;true&lt;/td&gt;
    &lt;td&gt;Automatically evals the content of &lt;code&gt;Ajax.Response#responseText&lt;/code&gt; and populates &lt;code&gt;Ajax.Response#responseJSON&lt;/code&gt; with it if the content-type returned by the server is set to &lt;code&gt;application/json&lt;/code&gt;. If the request doesn't obey &lt;a href=&quot;http://en.wikipedia.org/wiki/Same_origin_policy&quot;&gt;&lt;abbr title=&quot;Same Origin Policy&quot;&gt;SOP&lt;/abbr&gt;&lt;/a&gt;, the content is sanitized before evaluation. If you need to force evalutation, pass &lt;code&gt;'force'&lt;/code&gt;. To prevent it altogether, pass &lt;code&gt;false&lt;/code&gt;.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;sanitizeJSON&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;&lt;code&gt;false&lt;/code&gt; for local requests, &lt;code&gt;true&lt;/code&gt; otherwise.&lt;/td&gt;
    &lt;td&gt;Sanitizes the content of &lt;code&gt;Ajax.Response#responseText&lt;/code&gt; before evaluating it.&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;h3&gt;Common callbacks&lt;/h3&gt;

&lt;p&gt;When used on individual instances, all callbacks (except &lt;code&gt;onException&lt;/code&gt;) are invoked with two parameters: the &lt;code&gt;XMLHttpRequest&lt;/code&gt; object and the result of evaluating the &lt;code&gt;X-JSON&lt;/code&gt; response header, if any (can be &lt;code&gt;null&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;For another way of describing their chronological order and which callbacks are mutually exclusive, see &lt;a href=&quot;/api/ajax/request&quot;&gt;&lt;code&gt;Ajax.Request&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;table class=&quot;data&quot;&gt;
&lt;thead&gt;
  &lt;tr&gt;
    &lt;th&gt;Callback&lt;/th&gt;
    &lt;th&gt;Description&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onCreate&lt;/code&gt;&lt;br /&gt;&lt;span&gt;(v1.5.1)&lt;/span&gt;&lt;/th&gt;
    &lt;td&gt;Triggered when the &lt;code&gt;Ajax.Request&lt;/code&gt; object is initialized. This is &lt;em&gt;after&lt;/em&gt; the parameters and the URL have been processed, but &lt;em&gt;before&lt;/em&gt; first using the methods of the XHR object.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onComplete&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;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.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onException&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;Triggered whenever an XHR error arises.  Has a custom signature: the first argument is the requester (i.e. an &lt;code&gt;Ajax.Request&lt;/code&gt; instance), the second is the exception object.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onFailure&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;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 &lt;em&gt;before&lt;/em&gt; &lt;code&gt;onComplete&lt;/code&gt;.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onInteractive&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;(Not guaranteed) Triggered whenever the requester receives a part of the response (but not the final part), should it be sent in several packets.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onLoaded&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;(Not guaranteed) Triggered once the underlying XHR object is setup, the connection open, and ready to send its actual request.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onLoading&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;(Not guaranteed) Triggered when the underlying XHR object is being setup, and its connection opened.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onSuccess&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;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 &lt;em&gt;before&lt;/em&gt; &lt;code&gt;onComplete&lt;/code&gt;.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onUninitialized&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;(Not guaranteed) Invoked when the XHR object was just created.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;on&lt;/code&gt;&lt;em&gt;XYZ&lt;/em&gt;&lt;/th&gt;
    &lt;td&gt;With &lt;em&gt;XYZ&lt;/em&gt; 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 &lt;code&gt;onSuccess&lt;/code&gt; / &lt;code&gt;onFailure&lt;/code&gt;.  Happens &lt;em&gt;before&lt;/em&gt; &lt;code&gt;onComplete&lt;/code&gt;.&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;h3&gt;Responder callbacks&lt;/h3&gt;

&lt;p&gt;When used on responders, all callbacks (except &lt;code&gt;onException&lt;/code&gt; and &lt;code&gt;onCreate&lt;/code&gt;) are invoked with three parameters: the requester (i.e. the corresponding &quot;instance&quot; of &lt;code&gt;Ajax.Request&lt;/code&gt;) object, the &lt;code&gt;XMLHttpRequest&lt;/code&gt; object and the result of evaluating the &lt;code&gt;X-JSON&lt;/code&gt; response header, if any (can be &lt;code&gt;null&lt;/code&gt;).  They also execute in the context of the responder, bound to the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; reference.&lt;/p&gt;

&lt;table class=&quot;data&quot;&gt;
&lt;thead&gt;
  &lt;tr&gt;
    &lt;th&gt;Callback&lt;/th&gt;
    &lt;th&gt;Description&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onCreate&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;Triggered whenever a requester object from the &lt;code&gt;Ajax&lt;/code&gt; 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.&lt;td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onComplete&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;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.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onException&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;Triggered whenever an XHR error arises.  Has a custom signature: the first argument is the requester (i.e. an &lt;code&gt;Ajax.Request&lt;/code&gt; instance), the second is the exception object.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onInteractive&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;(Not guaranteed) Triggered whenever the requester receives a part of the response (but not the final part), should it be sent in several packets.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onLoaded&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;(Not guaranteed) Triggered once the underlying XHR object is setup, the connection open, and ready to send its actual request.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onLoading&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;(Not guaranteed) Triggered when the underlying XHR object is being setup, and its connection opened.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;onUninitialized&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;(Not guaranteed) Invoked when the XHR object was just created.&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-11-18:12497</id>
    <published>2006-11-18T18:07:00Z</published>
    <updated>2007-04-08T12:01:09Z</updated>
    <category term="Ajax"/>
    <link href="http://prototypejs.org./api/ajax/periodicalUpdater" rel="alternate" type="text/html"/>
    <title>Ajax.PeriodicalUpdater</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Ajax.PeriodicalUpdater(container, url[, options])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Periodically performs an AJAX request and updates a container&#8217;s contents based on the response text.  Offers a mechanism for &#8220;decay,&#8221; which lets it trigger at widening intervals while the response is unchanged.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Ajax.PeriodicalUpdater(container, url[, options])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Periodically performs an AJAX request and updates a container&#8217;s contents based on the response text.  Offers a mechanism for &#8220;decay,&#8221; which lets it trigger at widening intervals while the response is unchanged.&lt;/p&gt;
&lt;p&gt;This object addresses the common need of periodical update, which is used by all sorts of &#8220;polling&#8221; mechanisms (e.g. in an online chatroom or an online mail client).&lt;/p&gt;

&lt;p&gt;The basic idea is to run a regular &lt;a href=&quot;/api/ajax/updater&quot;&gt;&lt;code&gt;Ajax.Updater&lt;/code&gt;&lt;/a&gt; at regular intervals, monitoring changes in the response text if the &lt;code&gt;decay&lt;/code&gt; option (see below) is active.&lt;/p&gt;

&lt;h3&gt;Additional options&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Ajax.PeriodicalUpdater&lt;/code&gt; features all the &lt;a href=&quot;/api/ajax/options&quot;&gt;common options&lt;/a&gt; and callbacks, plus those added by &lt;a href=&quot;/api/ajax/updater&quot;&gt;&lt;code&gt;Ajax.Updater&lt;/code&gt;&lt;/a&gt;.  It also provides two new options that deal with the original period, and its decay rate (how Rocket Scientist does that make us sound, uh?!).&lt;/p&gt;

&lt;table&gt;
&lt;thead&gt;
  &lt;tr&gt;
    &lt;th&gt;Option&lt;/th&gt;
    &lt;th&gt;Default&lt;/th&gt;
    &lt;th&gt;Description&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;frequency&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;&lt;code&gt;2&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;Okay, this is not a frequency (e.g 0.5Hz), but a period (i.e. a number of seconds).  Don&#8217;t kill me, I didn&#8217;t write this one!  This is the minimum interval at which AJAX requests are made.  You don&#8217;t want to make it too short (otherwise you may very well end up with multiple requests in parallel, if they take longer to process and return), but you technically can provide a number below one, e.g. 0.75 second.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;decay&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;This controls the rate at which the request interval grows when the response is unchanged.  It is used as a multiplier on the current period (which starts at the original value of the &lt;code&gt;frequency&lt;/code&gt; parameter).  Every time a request returns an unchanged response text, the current period is multiplied by the decay.  Therefore, the default value means regular requests (no change of interval).  Values higher than one will yield growing intervals.  Values below one are dangerous: the longer the response text stays the same, the more often you&#8217;ll check, until the interval is so short your browser is left with no other choice than suicide.  Note that, as soon as the response text &lt;em&gt;does&lt;/em&gt; change, the current period resets to the original one.&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;To better understand decay, here is a small sequence of calls from the following example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
new Ajax.PeriodicalUpdater('items', '/items', {
  method: 'get', frequency: 3, decay: 2
});
&lt;/code&gt;&lt;/pre&gt;

&lt;table&gt;
&lt;thead&gt;
  &lt;tr&gt;
    &lt;th&gt;Call#&lt;/th&gt;
    &lt;th&gt;When?&lt;/th&gt;
    &lt;th&gt;Decay before&lt;/th&gt;
    &lt;th&gt;Response changed?&lt;/th&gt;
    &lt;th&gt;Decay after&lt;/th&gt;
    &lt;th&gt;Next period&lt;/th&gt;
    &lt;th&gt;Comments&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;00:00&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;n/a&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
    &lt;td&gt;Response is deemed changed, since there is no prior response to compare to!&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;00:03&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;yes&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
    &lt;td&gt;Response did change again: we &#8220;reset&#8221; to 1, which was already the decay.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;3&lt;/td&gt;
    &lt;td&gt;00:06&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;no&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;6&lt;/td&gt;
    &lt;td&gt;Response didn&#8217;t change: decay augments by the &lt;code&gt;decay&lt;/code&gt; option factor: we&#8217;re waiting longer now&#8230;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;4&lt;/td&gt;
    &lt;td&gt;00:12&lt;/td&gt;
    &lt;td&gt;2&lt;/td&gt;
    &lt;td&gt;no&lt;/td&gt;
    &lt;td&gt;4&lt;/td&gt;
    &lt;td&gt;12&lt;/td&gt;
    &lt;td&gt;Still no change, doubling again.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;5&lt;/td&gt;
    &lt;td&gt;00:24&lt;/td&gt;
    &lt;td&gt;4&lt;/td&gt;
    &lt;td&gt;no&lt;/td&gt;
    &lt;td&gt;8&lt;/td&gt;
    &lt;td&gt;24&lt;/td&gt;
    &lt;td&gt;Jesus, is this thing going to change or what?&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;6&lt;/td&gt;
    &lt;td&gt;00:48&lt;/td&gt;
    &lt;td&gt;8&lt;/td&gt;
    &lt;td&gt;yes&lt;/td&gt;
    &lt;td&gt;1&lt;/td&gt;
    &lt;td&gt;3&lt;/td&gt;
    &lt;td&gt;Ah, finally!  Resetting decay to 1, and therefore using the original period.&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;h3&gt;Disabling and re-enabling a &lt;code&gt;PeriodicalUpdater&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;You can pull the brake on a running &lt;code&gt;PeriodicalUpdater&lt;/code&gt; by simply calling its &lt;code&gt;stop&lt;/code&gt; method.  If you wish to re-enable it later, just call its &lt;code&gt;start&lt;/code&gt; method.  Both
 take no argument.&lt;/p&gt;

&lt;h3&gt;Beware!  Not a specialization!&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Ajax.PeriodicalUpdater&lt;/code&gt; is not a specialization of &lt;a href=&quot;/api/ajax/updater&quot;&gt;&lt;code&gt;Ajax.Updater&lt;/code&gt;&lt;/a&gt;, despite its name.  When using it, do not expect to be able to use methods normally provided by &lt;a href=&quot;/api/ajax/request&quot;&gt;&lt;code&gt;Ajax.Request&lt;/code&gt;&lt;/a&gt; and &#8220;inherited&#8221; by &lt;code&gt;Ajax.Updater&lt;/code&gt;, such as &lt;code&gt;evalJSON&lt;/code&gt; or &lt;code&gt;getHeader&lt;/code&gt;.  Also the &lt;code&gt;onComplete&lt;/code&gt; callback is hijacked to be used for update management, so if you wish to be notified of every successful request, use &lt;code&gt;onSuccess&lt;/code&gt; instead (beware: it will get called &lt;em&gt;before&lt;/em&gt; the update is performed).&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-11-18:12496</id>
    <published>2006-11-18T18:06:00Z</published>
    <updated>2008-01-24T11:34:00Z</updated>
    <category term="Ajax"/>
    <link href="http://prototypejs.org./api/ajax/updater" rel="alternate" type="text/html"/>
    <title>Ajax.Updater</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Ajax.Updater(container, url[, options])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Performs an AJAX request and updates a container&#8217;s contents based on the response text.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Ajax.Updater(container, url[, options])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Performs an AJAX request and updates a container&#8217;s contents based on the response text.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Ajax.Updater&lt;/code&gt; is a specialization of &lt;a href=&quot;/api/ajax/request&quot;&gt;&lt;code&gt;Ajax.Request&lt;/code&gt;&lt;/a&gt;: everything about the latter is true for the former.  If you&#8217;re unfamiliar with &lt;code&gt;Ajax.Request&lt;/code&gt;, go read its documentation before going ahead with this one.&lt;/p&gt;

&lt;h3&gt;A simple example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
new Ajax.Updater('items', '/items', {
  parameters: { text: $F('text') }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;A note about timing&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;onComplete&lt;/code&gt; callback will get invoked &lt;strong&gt;after&lt;/strong&gt; the update takes place.&lt;/p&gt;

&lt;h3&gt;Additional options&lt;/h3&gt;

&lt;p&gt;Since the goal of &lt;code&gt;Ajax.Updater&lt;/code&gt; is specifically to update the contents of a DOM element (&lt;code&gt;container&lt;/code&gt;) with the response text brought back by the AJAX request, it features a couple of new options, in addition to &lt;a href=&quot;/api/ajax/options&quot;&gt;the common options set&lt;/a&gt;.  These are:&lt;/p&gt;

&lt;table&gt;
&lt;thead&gt;
  &lt;tr&gt;
    &lt;th&gt;Option&lt;/th&gt;
    &lt;th&gt;Default&lt;/th&gt;
    &lt;th&gt;Description&lt;/th&gt;
  &lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;evalScripts&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;
    &lt;td&gt;This determines whether &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; elements in the response text are evaluated or not.&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;code&gt;insertion&lt;/code&gt;&lt;/th&gt;
    &lt;td&gt;None&lt;/td&gt;
    &lt;td&gt;By default, &lt;a href=&quot;/api/element/methods/update&quot;&gt;&lt;code&gt;Element.update&lt;/code&gt;&lt;/a&gt; is used, which replaces the whole contents of the container with the response text.  You may want to instead insert the response text around existing contents.  Prior to version 1.6.0, you just needed to pass a valid &lt;a href=&quot;/api/insertion&quot;&gt;&lt;code&gt;Insertion&lt;/code&gt;&lt;/a&gt; object for this, such as &lt;a href=&quot;/api/insertion/bottom&quot;&gt;&lt;code&gt;Insertion.Bottom&lt;/code&gt;&lt;/a&gt;. As of Prototype 1.6.0, this notation is deprecated. Simply pass either &lt;code&gt;'top'&lt;/code&gt;, &lt;code&gt;'bottom'&lt;/code&gt;, &lt;code&gt;'before'&lt;/code&gt; or &lt;code&gt;'after'&lt;/code&gt; as a string instead.&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;In the following example, we assume that creating a new item through AJAX returns an XHTML fragment representing only the new item, which we need to add within our list container, but at the bottom of its existing contents.  Here it goes:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
new Ajax.Updater('items', '/items', {
  parameters: { text: $F('text') },
  insertion: Insertion.Bottom
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;About &lt;code&gt;evalScripts&lt;/code&gt; and defining functions&lt;/h3&gt;

&lt;p&gt;If you use &lt;code&gt;evalScripts: true&lt;/code&gt;, any &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; block will be evaluated.  This &lt;strong&gt;does not&lt;/strong&gt; mean it will get included in the page: they won't. Their content will simply be passed to the native &lt;code&gt;eval()&lt;/code&gt; function.  There are two consequences to this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The local scope will be that of Prototype's internal processing function.  Anything in your script declared with &lt;em&gt;&lt;code&gt;var&lt;/code&gt;&lt;/em&gt; will be discarded momentarily after evaluation, and at any rate will be invisible to the remainder of the page scripts.&lt;/li&gt;
&lt;li&gt;If you define functions in there, you need to actually &lt;strong&gt;create&lt;/strong&gt; them, otherwise they won't be accessible to the remainder of the page scripts.  That is, the following code won't work:&lt;/li&gt;
&lt;/ul&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
// This kind of script won't work if processed by Ajax.Updater:
function coolFunc() {
  // Amazing stuff!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You will need to use the following syntax:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
// This kind of script WILL work if processed by Ajax.Updater:
coolFunc = function() {
  // Amazing stuff!
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&#8217;s a common trickster, biting beginners in the ankle. So watch out!&lt;/p&gt;

&lt;h3&gt;Single container, or success/failure alternative?&lt;/h3&gt;

&lt;p&gt;The examples above all assume you&#8217;re going to update the same container whether your request succeeds or fails.  There may very well be times when you don&#8217;t want that.  You may want to update only for successful requests, or update a different container on failed requests.&lt;/p&gt;

&lt;p&gt;To achieve this, you can pass an object instead of a DOM element for the &lt;code&gt;container&lt;/code&gt; parameter.  This object &lt;strong&gt;must&lt;/strong&gt; exhibit a &lt;code&gt;success&lt;/code&gt; property, whose value is 
the container to be updated upon successful requests.  If you also provide it with a &lt;code&gt;failure&lt;/code&gt; property, its value will be used as the container for failed requests.&lt;/p&gt;

&lt;p&gt;In the following code, only successful requests get an update:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
new Ajax.Updater({ success: 'items' }, '/items', {
  parameters: { text: $F('text') },
  insertion: Insertion.Bottom
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The next example assumes failed requests will feature an error message as response text, and will go on to update another element with it, probably a status zone.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
new Ajax.Updater({ success: 'items', failure: 'notice' }, '/items', {
  parameters: { text: $F('text') },
  insertion: Insertion.Bottom
});
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://prototypejs.org./">
    <author>
      <name>Andrew</name>
    </author>
    <id>tag:prototypejs.org.,2006-11-18:12495</id>
    <published>2006-11-18T18:05:00Z</published>
    <updated>2008-01-25T23:52:42Z</updated>
    <category term="Ajax"/>
    <link href="http://prototypejs.org./api/ajax/request" rel="alternate" type="text/html"/>
    <title>Ajax.Request</title>
<summary type="html">&lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Ajax.Request(url[, options])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Initiates and processes an AJAX request.&lt;/p&gt;</summary><content type="html">
            &lt;pre&gt;&lt;code class=&quot;ebnf&quot;&gt;new Ajax.Request(url[, options])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Initiates and processes an AJAX request.&lt;/p&gt;
&lt;p&gt;This object is a general-purpose AJAX requester: it handles the life-cycle of the request, handles the boilerplate, and lets you plug in callback functions for your custom needs.&lt;/p&gt;

&lt;p&gt;In the optional options hash, you usually provide a &lt;code&gt;onComplete&lt;/code&gt; and/or &lt;code&gt;onSuccess&lt;/code&gt; callback, unless you're in the edge case where you're getting a JavaScript-typed response, that will automatically be &lt;code&gt;eval&lt;/code&gt;'d.&lt;/p&gt;

&lt;p&gt;For a full list of common options and callbacks, see &lt;a href=&quot;/api/ajax/options&quot;&gt;Ajax Options&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The only proper way to create a requester is through the &lt;strong&gt;&lt;code&gt;new&lt;/code&gt;&lt;/strong&gt; operator.  As soon as the object is created, it initiates the request, then goes on processing it throughout its life-cyle.&lt;/p&gt;

&lt;h3&gt;A basic example&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
var url = '/proxy?url=' + encodeURIComponent('http://www.google.com/search?q=Prototype');
// notice the use of a proxy to circumvent the Same Origin Policy.

new Ajax.Request(url, {
  method: 'get',
  onSuccess: function(transport) {
    var notice = $('notice');
    if (transport.responseText.match(/href=&quot;http:\/\/prototypejs.org/))
      notice.update('Yeah! You are in the Top 10!').setStyle({ background: '#dfd' });
    else
      notice.update('Damn! You are beyond #10...').setStyle({ background: '#fdd' });
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Request life-cycle&lt;/h3&gt;

&lt;p&gt;Underneath our nice requester objects lies, of course, &lt;code&gt;XMLHttpRequest&lt;/code&gt;.  The defined life-cycle is as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Created&lt;/li&gt;
&lt;li&gt;Initialized&lt;/li&gt;
&lt;li&gt;Request sent&lt;/li&gt;
&lt;li&gt;Response being received (can occur many times, as packets come in)&lt;/li&gt;
&lt;li&gt;Response received, request complete&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you can see in &lt;a href=&quot;/api/ajax/options&quot;&gt;Ajax options&lt;/a&gt;, Prototype's AJAX objects define a whole slew of callbacks, which are triggered in the following order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;onCreate&lt;/code&gt; (this is actually a callback reserved to &lt;a href=&quot;/api/ajax/responders&quot;&gt;AJAX global responders&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;onUninitialized&lt;/code&gt; (maps on Created)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;onLoading&lt;/code&gt; (maps on Initialized)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;onLoaded&lt;/code&gt; (maps on Request sent)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;onInteractive&lt;/code&gt; (maps on Response being received)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;on&lt;/code&gt;&lt;em&gt;XYZ&lt;/em&gt; (numerical response status code), onSuccess or onFailure (see below)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;onComplete&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The two last steps both map on &lt;em&gt;Response received&lt;/em&gt;, in that order.  If a status-specific callback is defined, it gets invoked.  Otherwise, if &lt;code&gt;onSuccess&lt;/code&gt; is defined and the response is deemed a success (see below), it is invoked.  Otherwise, if &lt;code&gt;onFailure&lt;/code&gt; is defined and the response is &lt;em&gt;not&lt;/em&gt; deemed a sucess, it is invoked.  Only after that potential first callback is &lt;code&gt;onComplete&lt;/code&gt; called.&lt;/p&gt;

&lt;h4&gt;A note on portability&lt;/h4&gt;

&lt;p&gt;Depending on how your browser implements &lt;code&gt;XMLHttpRequest&lt;/code&gt;, one or more callbacks may never be invoked.  In particular, &lt;code&gt;onLoaded&lt;/code&gt; and &lt;code&gt;onInteractive&lt;/code&gt; are not a 100% safe bet so far.  However, the global &lt;code&gt;onCreate&lt;/code&gt;, &lt;code&gt;onUninitialized&lt;/code&gt; and the two final steps are very much guaranteed.&lt;/p&gt;

&lt;h3&gt;&lt;code&gt;onSuccess&lt;/code&gt; and &lt;code&gt;onFailure&lt;/code&gt;, the under-used callbacks&lt;/h3&gt;

&lt;p&gt;Way too many people use &lt;code&gt;Ajax.Request&lt;/code&gt; in a similar manner to raw XHR, defining only an &lt;code&gt;onComplete&lt;/code&gt; callback even when they're only interested in &quot;successful&quot; responses, thereby testing it by hand:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
// This is too bad, there's better!
new Ajax.Request('/your/url', {
  onComplete: function(transport) {
    if (200 == transport.status)
      // yada yada yada
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First, as described below, you could use better &quot;success&quot; detection: success is generally defined, HTTP-wise, as either no response status or a &quot;2xy&quot; response status (e.g., 201 is a success, too).  See the example below.&lt;/p&gt;

&lt;p&gt;Second, you could dispense with status testing altogether!  Prototype adds callbacks specific to success and failure, which we listed above.  Here's what you could do if you're only interested in success, for instance:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
new Ajax.Request('/your/url', {
  onSuccess: function(transport) {
      // yada yada yada
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Automatic JavaScript response evaluation&lt;/h3&gt;

&lt;p&gt;If an ajax request follows the &lt;a href=&quot;http://en.wikipedia.org/wiki/Same_origin_policy (or SOP&quot;&gt;Same Origin Policy&lt;/a&gt; &lt;em&gt;and&lt;/em&gt; it's response has a JavaScript-related content-type, the content of the &lt;code&gt;responseText&lt;/code&gt; property will automatically be passed to &lt;code&gt;eval&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;What this means is, you don't even need to provide a callback to leverage pure-JavaScript AJAX responses.  That's pretty cool, wouldn't you say?  The list of JavaScript-related MIME types handled by Prototype is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;application/ecmascript&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;application/javascript&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;application/x-ecmascript&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;application/x-javascript&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/ecmascript&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/javascript&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/x-ecmascript&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;text/x-javascript&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The MIME type string is examined in a case-insensitive manner.&lt;/p&gt;

&lt;h3&gt;Methods you may find useful&lt;/h3&gt;

&lt;p&gt;Instances of the Request object provide several methods that can come in handy in your callback functions, especially once the request completed.&lt;/p&gt;

&lt;h4&gt;Is the response a successful one?&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;success()&lt;/code&gt; method examines the XHR's &lt;code&gt;status&lt;/code&gt; property, and follows general HTTP guidelines: unknown status is deemed successful, as is the whole 2xy status code family.  It's a generally better way of testing your response than the usual &lt;code&gt;200 == transport.status&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;Getting HTTP response headers&lt;/h4&gt;

&lt;p&gt;While you can obtain response headers from the XHR object, using its &lt;code&gt;getResponseHeader&lt;/code&gt; method, this makes for slightly verbose code, and several implementations may raise an exception when the header is not found. To make this easier, you can use the &lt;code&gt;Ajax.Response#getHeader&lt;/code&gt; method, which just delegates to the longer version and returns &lt;code&gt;null&lt;/code&gt; should an exception occur:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
new Ajax.Request('/your/url', {
  onSuccess: function(response) {
    // Note how we brace against null values
    if ((response.getHeader('Server') || '').match(/Apache/))
      ++gApacheCount;
    // Remainder of the code
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Evaluating JSON headers&lt;/h4&gt;

&lt;p&gt;Some backends will return JSON not as response text, but in the &lt;code&gt;X-JSON&lt;/code&gt; header. In which case, you don't even need to evaluate the returned JSON yourself, as Prototype automatically does so and passes it as the &lt;code&gt;headerJSON&lt;/code&gt; property of the &lt;code&gt;Ajax.Response&lt;/code&gt; object. Note that if there is no such header or its contents is invalid, this property will be set to &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;
new Ajax.Request('/your/url', {
  onSuccess: function(transport) {
    transport.headerJSON
  }
});
&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
</feed>
