TAILIEUCHUNG - Publishing AJAX and PHP - part 6

Một lần nữa chúng tôi thành công có thể sử dụng try / catch để xử lý các lỗi có thể xảy ra khi bắt đầu một kết nối đến máy chủ, hoặc trong khi đọc những phản hồi từ máy chủ. Một phiên bản an toàn hơn phương pháp handleRequestStateChange trông như thế này: | Client-Side Techniques with Smarter JavaScript use to read an XML response as a DOM object do something with the response Once again we can successfully use try catch to handle errors that could happen while initiating a connection to the server or while reading the response from the server. A safer version of the handleRequeststatechange method looks like this function executed when the state of the request changes function handleRequestStateChange continue if the process is completed if 4 continue only if HTTP status is OK if 200 try retrieve the response response do something with the response . . catch e display error message alert Error reading the response else e .toStri ng display status message alert There was a problem retrieving the data n OK let s see how these functions work in action. Time for Action Making Asynchronous Calls with XMLHttpRequest 1. In the foundations folder create a subfolder named async. 2. In the async folder create a file called async .txt and add the following text to it Hello client 3. In the same folder create a file called async .html and add the following code to it DOCTYPE html PUBLIC - W3C DTD XHTML EN http TR xhtml11 DTD html head title AJAX Foundations Using XMLHttpRequest title script type text javascript src script head body onload process Hello server 50 Chapter 2 br div id myDivElement body html 4. Create a file called async .js with the following contents holds an instance of XMLHttpRequest var xmlHttp createXmlHttpRequestObject creates an XMLHttpRequest instance function createXmlHttpRequestObject will store the reference to the XMLHttpRequest object var xmlHttp this should work for all browsers except IE6 and older try try to create XMLHttpRequest object xmlHttp new XMLHttpRequest catch e assume IE6 or older var XmlHttpVersions new Array .

TỪ KHÓA LIÊN QUAN
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.