tailieunhanh - Beginning Google Maps Applications with PHP and Ajax From Novice to Professional PHẦN 3

bạn phải sử dụng onreadystatechange () phương pháp khi readyState là tăng cường hơn nữa các ứng dụng web của bạn, bạn có thể sử dụng các trạng thái khác nhau của đối tượng yêu cầu để thêm Mẹo tải, hoàn thành, và các tiểu bang lỗi. | 54 CHAPTER 3 INTERACTING WITH THE USER AND THE SERVER alert Process Response If you run the script over and over sometimes the alert boxes will appear in the order Process Response then Continue but more likely you ll get Continue then Process Request. Just remember that if you want something to occur after the response you must use the onreadystatechange method when the readyState is 4. rip To further enhance your web application you could use the various states of the request object to add loading completed and error states. For example when initiating a request you could show an animated loading image to indicate to the user that the information is loading. Providing feedback at each stage of the request makes it easier for the user to know what s happening and how to react. If no loading state is displayed users may assume they have not actually clicked the button or will become frustrated and think nothing is happening. Using GDownloadUrl for Simple Ajax Requests If your web application doesn t require a high level of control over the Ajax request you can use an alternative object called GDownloadUrl. You can use GDownloadUrl to send and retrieve content the same way you do with GXmlHttp however the API is much simpler. Rather than checking response states and all that other stuff you just supply a URL with any appropriate GET variables and a function to execute when the response in returned. This simplifies the request to the following GDownloadUrl getVars function data responseCode Do something with the data But note that this approach doesn t give you as much control over the different states of the request. Parsing the XML Document Using DOM Methods When the readyState reaches 4 and your onreadystatechange function is triggered you need to parse the response from the server to determine if the PHP script replied with an execution error or a successful save. Referring back to Listing 3-6 the source you can see that in the .