Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Practical prototype and scipt.aculo.us part 12: The information in this book is distributed on an "as is" basis, without warranty Although every pre-caution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. | 54 CHAPTER 4 AJAX ADVANCED CLIENT SERVER COMMUNICATION Figure 4-3 shows the results. This time you ll see two dialogs the original pancakes and the one inside the line highlighted in the previous code block complete. Figure 4-3. These two dialogs appear in sequence. So by adding just a little code to this example you ve learned two more things The onComplete option is a new property in our options object. It sets up a callback a function that will run at a certain point in the future. An Ajax request keeps the browser updated on its progress triggering several different ready states along the way. In this case our onComplete function will be called when the request is complete. The pancakes dialog appears before the complete dialog so you can deduce that the onComplete function is called after the response is evaluated. Let s use another callback. Replace onComplete with onSuccess see Figure 4-4 new Ajax.Request ajax.js method get onSuccess function alert success CHAPTER 4 AJAX ADVANCED CLIENT SERVER COMMUNICATION 55 Figure 4-4. These two dialogs appear in sequence. Figure 4-4 is subtly different than Figure 4-3. Like before you ll see two dialog boxes but this time the pancakes dialog comes last. So you can assume the following The onSuccess option is a callback that will be run if the request is a success. If there s a failure of some kind a 404 error a communication error an internal server error etc. its companion onFailure will get called instead. Since we saw the callback s alert dialog first we know that onSuccess and onFailure are called before the remote JavaScript file is evaluated and also before onComplete. True to its name onComplete is called as the very last thingAjax.Request does before it punches its timecard. But it decides between calling onSuccess or onFailure as soon as it knows the outcome of the request. We can request any kind of file with Ajax not just JavaScript files. To prove it rename ajax.js to ajax.txt and try this see Figure 4-5 new