Ajax核心知識總結,ajax核心總結
Ajax的原理
通過XMLHttpRequest對象來向伺服器發送非同步請求,從伺服器獲得資料,用JavaScript來操作DOM從而更新頁面。
特點
- 提交:不用向伺服器提交整個頁面
- 返回:不再是整個頁面,而是XML,JSON等資料形式
- 效果:局部更新網頁。
XMLHttpRequest對象是Ajax技術的核心。
功能
一套可以在Javascript等指令碼語言中通過http協議傳送或接收XML及其他資料的一套API。
用法步驟:
在做項目的過程中,搜尋方塊的聯想功能中用到了Ajax技術,因為當時這個聯想功能是屬於公用部分,一個人做好,其他系統直接用,而且自己當時沒學習Ajax,所以就直接按著文檔去實現。現在趁著總結,結合這個聯想功能的js檔案,來總結XMLHttpRequest的五步使用法。
/*1.針對不同瀏覽器建立XMLHttpRequest對象*/ if(window.XMLHttpRequest){ //alert("IE7,IE8,FireFox"); xmlhttp =new XMLHttpRequest(); //針對某些特定版本的mozillar瀏覽器的BUG進行修正 //具體來說:瀏覽器是通過MIME Type來決定什麼內容用什麼形式顯示 //如果來自伺服器的響應沒有 XML mime-type 頭部,則一些版本的 Mozilla 瀏覽器不能正常運行。 //對於這種情況,httpRequest.overrideMimeType('text/xml'); 語句將覆蓋發送給伺服器的頭部,強制 text/xml 作為 mime-type。 if(xmlhttp.overrideMimeType){ xmlhttp.overrideMimeType("text/xml"); } }else if(window.ActiveXObject){ //alert("IE6,IE5.5,IE5"); var activexName=["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP","Miscrosoft XMLHTTP"]; for(var i=0;i<activexName.length;i++) { try{ xmlhttp=new ActiveXObject(activexName[i]); break; }catch(e){ } } } if(xmlhttp == undefined || xmlhttp == null){ alert("當前瀏覽器不支援建立XMLHttpRequest對象,請更換瀏覽器"); return; } /*2.在XMLHttpRequest對象上註冊回呼函數*/
xmlhttp.onreadystatechange=callback; //XMLHttpRequest對象有readyState屬性和onreadystatechange屬性(函數),當readyState屬性改變時,就會調用onreadystatechange// 0: 請求未初始化// 1: 伺服器串連已建立// 2: 請求已接收// 3: 請求處理中// 4: 請求已完成,且響應已就緒//當onreadystatechange觸發時,就會調用callback函數/*3.使用open方法設定和伺服器端互動的基本資料*/
//GET方式互動 xmlhttp.open("GET","AJAX?name=" + userName,true); //POST方式互動 xmlhttp.open("POST","AJAX",true); //POST方式互動所需增加的代碼 xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
/*4.設定發送的資料,開始和伺服器端互動 */
//GET方式 xmlhttp.send(null); //POST方式 xmlhttp.send("name=" + userName); /*5.更新介面*///在回呼函數中判斷互動是否結束,響應是否正確,並根據需要擷取伺服器返回的資料,並更新頁面function callback(){//判斷和伺服器端的互動是否完成,還要判斷伺服器端是否正確返回了資料 if(xmlhttp.readyState == 4){ //表示和伺服器端的互動已經完成 if(xmlhttp.status == 200){ //表示伺服器的是響應代碼是200,正確返回了資料 var message=xmlhttp.responseText; //XML資料對應的DOM對象的接受方法 //使用的前提是,伺服器端需要設定contenttype為text/xml //記憶像div標籤中填充常值內容的方法 var div=document.getElementById("message"); div.innerHTML=message; } }
五步法封裝: 在實際使用XMLHTTPRequest當中,不能每次都建立對象,我們需要將公用部分抽象成“類”,在用的時候,我們直接調用對象的屬性,方法,並傳入相應的參數即可。
//建立一個"類",這個"類"具有XMLHTTP屬性function CallBackObject(){ this.XmlHttp = this.GetHttpObject();}// 通過原型設定這個類的其他屬性://通過GetHTTPObject獲得XMLHTTPRequest對象,此屬性中封裝的是第一步CallBackObject.prototype.GetHttpObject = function(){ var xmlhttp; // if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { // try { // xmlhttp = new XMLHttpRequest(); // } catch (e) { // xmlhttp = false; // } // } try{ //對於Firefox和Opera等瀏覽器 xmlhttp=new XMLHttpRequest(); //有此版本的Mozilla瀏覽器在處理伺服器返回的包含XML mime-type頭部資訊內容時會出錯。 //所以,為了確保返回內容是text/xml資訊,需要包含下面的語句。 if(xmlhttp.overrideMimeType) { xmlhttp.overrideMimeType("text/xml"); } }catch(e){ //對於IE瀏覽器 var activexName=new Array('Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Micrsoft.XMLHTTP'); var success=false; for(var i=0;i<activexName.length && !success;i++){ try{ xmlhttp=new ActiveXObject(activexName[i]); success=true; break; }catch(e){ } } if(!success){ alert('Unable to create XMLHttpRequest.'); } } return xmlhttp; } } //DoCallBack封裝第二步,第三和第四步CallBackObject.prototype.DoCallBack = function(URL){ if( this.XmlHttp ) { if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 ) { var oThis = this; this.XmlHttp.open('POST', URL); this.XmlHttp.onreadystatechange = function(){ oThis.ReadyStateChange(); }; this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); this.XmlHttp.send(null); } }} CallBackObject.prototype.AbortCallBack = function(){ if( this.XmlHttp ) this.XmlHttp.abort();} CallBackObject.prototype.OnLoading = function(){ // Loading} CallBackObject.prototype.OnLoaded = function(){ // Loaded} CallBackObject.prototype.OnInteractive = function(){ // Interactive} CallBackObject.prototype.OnComplete = function(responseText, responseXml){ // Complete} CallBackObject.prototype.OnAbort = function(){ // Abort} CallBackObject.prototype.OnError = function(status, statusText){ // Error} //封裝第五步,在回呼函數中判斷互動是否結束,響應是否正確,並根據需要擷取伺服器返回的資料,並更新頁面CallBackObject.prototype.ReadyStateChange = function(){ if( this.XmlHttp.readyState == 1 ) { this.OnLoading(); } else if( this.XmlHttp.readyState == 2 ) { this.OnLoaded(); } else if( this.XmlHttp.readyState == 3 ) { this.OnInteractive(); } else if( this.XmlHttp.readyState == 4 ) { if( this.XmlHttp.status == 0 ) this.OnAbort(); else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" ) this.OnComplete(this.XmlHttp.responseText, this.XmlHttp.responseXML); else this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText); }}