Đ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 14: 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. | 66 CHAPTER 4 AJAX ADVANCED CLIENT SERVER COMMUNICATION Since Firebug logs all Ajax requests you can see for yourself. Near the bottom of your console should be a gray box containing the URL of the request expand this box to view all the request s details as depicted in Figure 4-11. S GET http 192.168.1.106 andrew book chapter4 breakfast.php food_type waffles prototype.js line 1100 60ms Params Headers Response foodtype waffles taste delicious S GET http 192.168.1.106 andrew book chapter4 breakfastphp food_type waffles prototype.js line 1100 60ms Params Headers Response li At strong 3 41 pm on June 1 2007 strong I ate strong delicious waffles strong Figure 4-11. The details of our Ajax request That was fun. Let s try it again run the exact same command in the console see Figure 4-12 . Figure 4-12. What happened to the first one CHAPTER 4 AJAX ADVANCED CLIENT SERVER COMMUNICATION 67 Figure 4-12 is not quite what we expected. The time is different so the content got replaced properly. But we don t want to replace the contents of ul breakfast_history we want to add to what s already there. Typically breakfast log entries are arranged so that the most recent is first. So let s change our Ajax call so that new entries are appended to the top of the container new Ajax.Updater breakfast_history breakfast.php insertion top method get parameters food_type waffles taste delicious Run this code and you ll see your new entry added to the top of the list as in Figure 4-13. Each time you run this code in fact a new li will be added to the top of your ul container. Figure 4-13. New entries appear at the top. Handling Errors Our breakfast.php script works but it s not exactly battle-tested. It naively assumes that each request will have the two pieces of information it wants. What if something goes wrong What if our scrambled eggs fail to be tabulated We need to work out some sort of code between client and server to handle situations like these. 68 CHAPTER 4 AJAX ADVANCED CLIENT .