
网页设计外文翻译使用XMLHttpRequest对象.doc
13页Using the XMLHttpRequest ObjectNow that we’ve discussed the history of dynamic Web applications and introduced Ajax, it’s time to cover the heart of the matter: how to use the XMLHttpRequest object. While Ajax is more of a technique than a technology, without widespread support for XMLHttpRequest, Google Suggest and Ta-da List wouldn’t exist as we currently know them. And you wouldn’t be reading this book! XMLHttpRequest was originally implemented in Internet Explorer 5 as an ActiveX component. That it worked only in Internet Explorer kept most developers from using XMLHttpRequest until its recent adoption as a de facto standard in Mozilla 1.0 and Safari 1.2. It’s important to note that XMLHttpRequest is not a W3C standard, though much of the functionality is covered in a new proposal: the DOM Level 3 Load and Save Specification. Because it is not a standard, its behavior may differ slightly from browser to browser, though most methods and properties are widely supported. Currently, Firefox, Safari, Opera, Konqueror, and Internet Explorer all implement the behavior of the XMLHttpRequest object similarly. That said, if a significant number of your users still access your site or application with older browsers, you will need to consider your options. As we discussed in Chapter 1, if you are going to use Ajax techniques, you need to either develop an alternative site or allow your application to degrade gracefully. With most usage statistics indicating that only a small fraction of browsers in use today lack XMLHttpRequest support, the chances of this being a problem are slim. However, you need to check your Web logs and determine what clients your customers are using to access your sites. Overview of the XMLHttpRequest Object You must first create an XMLHttpRequest object using JavaScript before you can use the object to send requests and process responses. Since XMLHttpRequest is not a W3C standard, you can use JavaScript in a couple of ways to create an instance of XMLHttpRequest. Internet Explorer implements XMLHttpRequest as an ActiveX object, and other browsers such as Firefox, Safari, and Opera implement it as a native JavaScript object. Because of these differences, the JavaScript code must contain logic to create an instance of XMLHttpRequest using the ActiveX technique or using the native JavaScript object technique. The previous statement might send shivers down the spines of those who remember the days when the implementation of JavaScript and the DOM varied widely among browsers. Fortunately, in this case you don’t need elaborate code to identify the browser type to know how to create an instance of the XMLHttpRequest object. All you need to do is check the browser’s support of ActiveX objects. If the browser supports ActiveX objects, then you create the XMLHttpRequest object using ActiveX. Otherwise, you create it using the native JavaScript object technique. If the call to window.ActiveXObjectfails, then the JavaScript branches to the elsestatement, which determines whether the browser implements XMLHttpRequest as a native JavaScript object. If window.XMLHttpRequestexists, then an instance of XMLHttpRequest is created. Thanks to JavaScript’s dynamically typed nature and that XMLHttpRequest implementations are compatible across various browsers, you can access the properties and methods of an instance of XMLHttpRequest identically, regardless of the method used to create the instance. This greatly simplifies the development process and keeps the JavaScript free of browser-specific logic. Methods and Properties Table 2-1 shows some typical methods on the XMLHttpRequest object. Don’t worry; we’ll talk about these methods in greater detail in a moment. Table 2-1. Standard XMLHttpRequest OperationsMethodDescriptionabort()The current request.getAllResponseHeaders()Returns all the response headers for the HTTP request as key/value pairs.getResponseHeader("header")Returns the string value of the specified header.open("method", "url")Sets the stage for a call to the server. The method argument can be either GET, POST, or PUT. The url argument can be relative or absolute. This method includes three optional arguments. send(content)Sends the request to the server.setRequestHeader("header", "value")Sets the specified header to the supplied value. open() must be called before attempting to set any headers.Let’s take a closer look at these methods. void open(string method, string url, boolean asynch, string username, string password): This method sets up your call to the server. This method is meant to be the script-only method of initializing a request. It has two required arguments and three optional arguments. You are required to supply the specific method you are invoking (GET, POST, or PUT) and the URL of the resource you are calling. You may optionally pass a Boolean indicating whether this call is meant to be asynchronous—the default is true, whic。





![河南新冠肺炎文件-豫建科[2020]63号+豫建科〔2019〕282号](http://img.jinchutou.com/static_www/Images/s.gif)






