ebook img

Ajax The Complete Reference PDF

674 Pages·2008·18.56 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Ajax The Complete Reference

Ajax: The Complete Reference www.ebooks.org.in About the Author Thomas A. Powell ([email protected]) has been involved in the Internet community for almost 20 years. In the early 1990s he worked for the first Internet service provider in Southern California, CERFnet. In 1994 he founded PINT, Inc. (pint.com) a Web development firm with headquarters in San Diego, which today services a diverse range of educational and corporate clients around the country. He is also the lead investor and founder of port80 Software (port80software.com), which sells Web server and development products to thousands of organizations around the world. In addition, Powell is the author of numerous other Web development books, including the bestsellers JavaScript: The Complete Reference, HTML & XHTML: The Complete Reference, Web Design: The Complete Reference, and Web Site Engineering. He also writes frequently about Web technologies for Network World magazine. Mr. Powell teaches Web design and development classes for the University of California, San Diego Computer Science and Engineering Department as well as for the Information Technologies program at the UCSD Extension. He holds a B.S. from UCLA and an M.S. in Computer Science from UCSD. About the Technical Editor Ric Smith is a principal product manager for Oracle’s Application Development Tools. He is responsible for the evangelism and product direction of Oracle ADF Faces Rich Client as well as Oracle’s Ajax and Java EE Web Tier offerings. Prior to joining Oracle’s Fusion Middleware team, Ric worked for Oracle’s consulting business as a principal consultant, specializing in Java EE and Ajax development. In addition, Ric is a frequent speaker at industry events and has written articles featured in industry publications such as Java Developer’s Journal and Ajax World magazine. He is also a member of the OpenAjax Alliance and a graduate of the University of Arizona. www.ebooks.org.in Ajax: The Complete Reference Thomas A. Powell New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto www.ebooks.org.in Copyright © 2008 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. 0-07-159662-3 The material in this eBook also appears in the print version of this title: 0-07-149216-X. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. For more information, please contact George Hoare, Special Sales, at [email protected] or (212) 904-4069. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MER- CHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. DOI: 10.1036/007149216X www.ebooks.org.in Professional Want to learn more? We hope you enjoy this McGraw-Hill eBook! If you’d like more information about this book, its author, or related books and websites, please click here. www.ebooks.org.in For more information about this title, click here Contents at a Glance Part I Core Ideas 1 Introduction to Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 Pre-Ajax JavaScript Communications Techniques . . . . . . . . . . . . . . . . . . . . 15 3 XMLHttpRequest Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4 Data Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Part II Applied Ajax 5 Developing an Ajax Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 6 Networking Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 7 Security Concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 8 User Interface Design for Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 9 Site and Application Architecture with Ajax . . . . . . . . . . . . . . . . . . . . . . . . 407 Part III Advanced Topics 10 Web Services and Beyond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483 Part IV Appendixes A JavaScript Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 B HTTP 1.1 Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 587 C AjaxTCR Library Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 609 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635 www.ebookvs.org.in This page intentionally left blank www.ebooks.org.in Contents Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Part I Core Ideas 1 Introduction to Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Ajax Defi ned . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Hello Ajax World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The Challenges of Running Ajax Examples Locally . . . . . . . . . . . . . . 11 The Rise of Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Implications of Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2 Pre-Ajax JavaScript Communications Techniques . . . . . . . . . . . . . . . . . . . . 15 One-way Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 One-way Image Object Technique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 One-way Iframes with Query Strings . . . . . . . . . . . . . . . . . . . . . . . . . . 22 One-way Script Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Other Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 One-way Iframes with Form Posts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Cookie-based Transport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Two-way Communications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Images for Two-way Communications . . . . . . . . . . . . . . . . . . . . . . . . 32 Two-way Script Tag Communication . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Less Common Two-way Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Two-way Iframes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Communications Pattern Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Example Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 A Client-Side JS Error Reporter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Offsite Link Press Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Fancy Ratings—Pre-Ajax Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 RSS Reader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3 XMLHttpRequest Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Overview of XHRs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Instantiation and Cross-Browser Concerns . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 ActiveX XHR Anxiety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 A Cross-Browser XHR Wrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 www.eboovkisi.org.in viii Ajax: The Complete Reference XHR Request Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Synchronous Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Asynchronous Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Sending Data via GET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Sending Data via Post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Request Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Other HTTP Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Head Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Response Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 readyState Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 status and statusText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 responseText . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 responseXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Response Headers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Controlling Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Authentication with XHRs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Propriety and Emerging XHR Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 Managing MIME Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Multipart Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 onProgress and Partial Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 Other Firefox Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 XHR Arcana and Future Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 XHR Implications and Challenges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Ajax and Memory Leaks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 4 Data Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Ajax and Character Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Data Format Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Encoding/Decoding Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Encoding/Decoding Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Security Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Transmission Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Standard Encoding: x-www-form-urlencoded . . . . . . . . . . . . . . . . . . . . . . . . 121 Extending Standard Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Form Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Object Serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Using Other Input Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Other Request Forms: YAML, Text, and Beyond . . . . . . . . . . . . . . . . . 136 File Attachments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Response Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Text Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Script Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 www.ebooks.org.in

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.