|
10 | 10 | * In the optional `options` hash, you usually provide an `onComplete` and/or
|
11 | 11 | * `onSuccess` callback, unless you're in the edge case where you're getting a
|
12 | 12 | * JavaScript-typed response, that will automatically be `eval`'d.
|
13 |
| - * |
| 13 | + * |
14 | 14 | * For a full list of common options and callbacks, see "Ajax options" heading
|
15 | 15 | * of the [[Ajax section]].
|
16 | 16 | *
|
|
23 | 23 | * });
|
24 | 24 | *
|
25 | 25 | * ##### Request life-cycle
|
26 |
| - * |
| 26 | + * |
27 | 27 | * Underneath our nice requester objects lies, of course, `XMLHttpRequest`. The
|
28 | 28 | * defined life-cycle is as follows:
|
29 |
| - * |
| 29 | + * |
30 | 30 | * 1. Created
|
31 | 31 | * 2. Initialized
|
32 | 32 | * 3. Request sent
|
33 | 33 | * 4. Response being received (can occur many times, as packets come in)
|
34 | 34 | * 5. Response received, request complete
|
35 |
| - * |
| 35 | + * |
36 | 36 | * As you can see under the "Ajax options" heading of the [[Ajax section]],
|
37 | 37 | * Prototype's AJAX objects define a whole slew of callbacks, which are
|
38 | 38 | * triggered in the following order:
|
39 |
| - * |
| 39 | + * |
40 | 40 | * 1. `onCreate` (this is actually a callback reserved to [[Ajax.Responders]])
|
41 | 41 | * 2. `onUninitialized` (maps on Created)
|
42 | 42 | * 3. `onLoading` (maps on Initialized)
|
43 | 43 | * 4. `onLoaded` (maps on Request sent)
|
44 | 44 | * 5. `onInteractive` (maps on Response being received)
|
45 | 45 | * 6. `on`*XYZ* (numerical response status code), onSuccess or onFailure (see below)
|
46 | 46 | * 7. `onComplete`
|
47 |
| - * |
| 47 | + * |
48 | 48 | * The two last steps both map on *Response received*, in that order. If a
|
49 | 49 | * status-specific callback is defined, it gets invoked. Otherwise, if
|
50 | 50 | * `onSuccess` is defined and the response is deemed a success (see below), it
|
51 | 51 | * is invoked. Otherwise, if `onFailure` is defined and the response is *not*
|
52 | 52 | * deemed a success, it is invoked. Only after that potential first callback is
|
53 | 53 | * `onComplete` called.
|
54 |
| - * |
| 54 | + * |
55 | 55 | * ##### A note on portability
|
56 |
| - * |
| 56 | + * |
57 | 57 | * Depending on how your browser implements `XMLHttpRequest`, one or more
|
58 | 58 | * callbacks may never be invoked. In particular, `onLoaded` and
|
59 | 59 | * `onInteractive` are not a 100% safe bet so far. However, the global
|
60 | 60 | * `onCreate`, `onUninitialized` and the two final steps are very much
|
61 | 61 | * guaranteed.
|
62 |
| - * |
| 62 | + * |
63 | 63 | * ##### `onSuccess` and `onFailure`, the under-used callbacks
|
64 |
| - * |
| 64 | + * |
65 | 65 | * Way too many people use [[Ajax.Request]] in a similar manner to raw XHR,
|
66 | 66 | * defining only an `onComplete` callback even when they're only interested in
|
67 | 67 | * "successful" responses, thereby testing it by hand:
|
68 |
| - * |
| 68 | + * |
69 | 69 | * // This is too bad, there's better!
|
70 | 70 | * new Ajax.Request('/your/url', {
|
71 | 71 | * onComplete: function(response) {
|
72 | 72 | * if (200 == response.status)
|
73 | 73 | * // yada yada yada
|
74 | 74 | * }
|
75 | 75 | * });
|
76 |
| - * |
| 76 | + * |
77 | 77 | * First, as described below, you could use better "success" detection: success
|
78 | 78 | * is generally defined, HTTP-wise, as either no response status or a "2xy"
|
79 | 79 | * response status (e.g., 201 is a success, too). See the example below.
|
80 |
| - * |
| 80 | + * |
81 | 81 | * Second, you could dispense with status testing altogether! Prototype adds
|
82 | 82 | * callbacks specific to success and failure, which we listed above. Here's
|
83 | 83 | * what you could do if you're only interested in success, for instance:
|
84 |
| - * |
| 84 | + * |
85 | 85 | * new Ajax.Request('/your/url', {
|
86 | 86 | * onSuccess: function(response) {
|
87 | 87 | * // yada yada yada
|
88 | 88 | * }
|
89 | 89 | * });
|
90 |
| - * |
| 90 | + * |
91 | 91 | * ##### Automatic JavaScript response evaluation
|
92 | 92 | *
|
93 | 93 | * If an Ajax request follows the _same-origin policy_ **and** its response
|
@@ -305,9 +305,10 @@ Ajax.Request = Class.create(Ajax.Base, {
|
305 | 305 |
|
306 | 306 | try {
|
307 | 307 | (this.options['on' + state] || Prototype.emptyFunction)(response, response.headerJSON);
|
308 |
| - Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON); |
309 | 308 | } catch (e) {
|
310 | 309 | this.dispatchException(e);
|
| 310 | + } finally { |
| 311 | + Ajax.Responders.dispatch('on' + state, this, response, response.headerJSON); |
311 | 312 | }
|
312 | 313 |
|
313 | 314 | if (state == 'Complete') {
|
|
0 commit comments