【正文】
ng 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. Inter 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 , then the JavaScript branches to the elsestatement, which determines whether the browser implements XMLHttpRequest as a native JavaScript object. If , then an instance of XMLHttpRequest is created. Thanks to JavaScript’s dynamically typed nature and that XMLHttpRequest implementations are patible 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 browserspecific logic. Methods and Properties Table 21 shows some typical methods on the XMLHttpRequest object. Don’t worry。 In general, the various frameworks and toolkits available on the Web take care of the basic wiring and the browser abstractions, and some add user interface ponents. Some are purely client based。由于只能在IE 中使用,所以大多數(shù)開發(fā)人員都沒有用 XMLHttpRequest,直到最近, Mozilla 和 Safari 把它采用為事實上的標準,情況才有改觀。 前面已經(jīng)說過,如果大量用戶還是在使用較舊的瀏覽器訪問網(wǎng)站或應用,就要三思了。 XMLHttpRequest對象概述 在使用 XMLHttpRequest 對象發(fā)送請求和處理響應之前,必須先用 JavaScript創(chuàng)建一個 XMLHttpRequest 對象。 很多人可能還記得從前的那段日子,那時不同瀏覽器上的 JavaScript 和 DOM實現(xiàn)簡直千差萬別,聽了上面這段話之后,這些人可能又會不寒而栗。否則,就要使用本地 JavaScript 對象技術來創(chuàng)建。這就大大簡化了開發(fā)過程,而且在JavaScript 中也不必編寫特定于瀏覽器的邏輯。 method參數(shù)可以是 GET、 POST或 PUT。 void open(string method, string url, boolean asynch, string username, string password):這個方法會建立對服務器的調(diào)用。另外還可以傳遞一個 Boolean 值,指示這個調(diào)用是異步的還是同步的。不過,前面已經(jīng)說過,在某些情況下這個參數(shù)設置為 false也是有用的,比如在持久存儲頁面之前可以先驗證用戶的輸入。可選參數(shù)可以是 DOM 對象的實例、輸入流,或 者串。需要說明,這個方法必須在調(diào)用open()之后才能調(diào)用。 string getAllResponseHeaders(): 這個方法的核心功能對 Web 應用開發(fā)人員應 該很熟悉了,它返回一個串,其中包含 HTTP 請求的所有響應首部,首部包括 Content Length、 Date和 URI。 表 22 標準 XMLHttpRequest 屬性 屬 性 描 述 onreadystatechange 每個狀態(tài)改變時都會觸發(fā)這個事件處理器,通常會調(diào)用一個 JavaScript 函數(shù) readyState 請求的狀態(tài)。 不同于標準 Web 客戶中所用的標準請求 /響應方法, Ajax 應用的做法稍有差別 。 圖 21 標準 Ajax 交互 2. 創(chuàng)建 XMLHttpRequest對象的一個實例??赡苷{(diào)用 servlet、 CGI 腳 本,或者任何服務器端技術。在另外一些更復雜示例中,響應可能涉及更廣,還包括 JavaScript、 DOM 管理以及其他相關的技術。 通常, Web 上提供的各種框架和工具包負責基本的連接和瀏覽器抽象,有些還增加了用戶界面組件。一些比較成熟的庫包括 libXmlRequest、 RSLite、 sarissa、JavaScript 對象注解( JavaScript Object Notation, JSON)、 JSRS、直接 Web 遠程通信( Direct Web Remoting, DWR)和 Rails on Ruby??梢钥吹?,這個對象相當簡單,無需你考慮其中很多的復雜性。 ? [3]呂廷杰 客戶關系管理與主題分析 北京:人民郵電出版社, 詳細地闡述了客戶關系管理 (CRM)的基本理論及其具體應用,在基本理論方面,除了敘述 CRM 的基本概念外,還著重介紹了 CRM