ajax在IE中無法正常測試原因的探索及學習

來源:互聯網
上載者:User

標籤:javascrip   自動   tomcat   efi   調用   支援   ons   寫法   注意   

愚蠢的測試

《JavaScript DOM編程藝術》P116,

問了一下別人,IE需要設定管理員 localhost:8080才能對這個網頁有響應。。。需要配置tomcat和Java JDK。配了一晚上!終於搞定了。這裡我討論4種情況。(可以把無伺服器的IE當成錯誤的結果)

 

1.無伺服器測試AJAX時,IE11中無法得出結果,只有空白的頁面。(錯誤原因:

request.open( "GET","example.txt",true );拒絕訪問)

伺服器中,IE中"Response Received"先於"function Done",響應快。頁面正常。

無論是否在伺服器中,Firefox中的"function Done"都先於"Response Received"。說明響應慢。頁面正常。

function getHTTPObject(){

if(typeof XMLHttpRequest == "undefined")

XMLHttpRequest = function(){

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

return false;

}

return new XMLHttpRequest();

}

 

2.改成如下,無伺服器,IE只彈出”1”,仍然是空白頁面。(錯誤原因:

request.open( "GET","example.txt",true );拒絕訪問)

伺服器中,IE中彈出順序為”1”,"Response Received","function Done"。響應快。頁面正常。

無論是否在伺服器中,Firefox的彈出順序都為”1”,"function Done","Response Received"。響應慢。頁面正常。

function getHTTPObject(){

alert("1");  //說明一定會運行到這裡

if(typeof XMLHttpRequest == "undefined")

XMLHttpRequest = function(){

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

return false;

}

return new XMLHttpRequest();

}

或者

function getHTTPObject(){

if(typeof XMLHttpRequest == "undefined")

XMLHttpRequest = function(){

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

return false;

}

alert("1");   //也能說明一定能運行這裡。

return new XMLHttpRequest();

}         //(無錯誤)

 

 

 

3.改成如下,放在if之後,IE中"Response Received"先於"function Done",說明響應快了。沒有彈2,頁面正常。

Firefox空白頁面。

function getHTTPObject(){

if(typeof XMLHttpRequest == "undefined")

alert("2");   //為什麼前面無伺服器IE無法正常顯示這裡卻可以了??簡直就像開了伺服器            

           //一樣。

           //只要IE的這裡沒有alert();就沒有反應,有就有反應。

           //只要Firefox這裡沒有alert();就有反應,和IE相反。這裡是關鍵點。

           //認真考慮了一下這句話,好像把if語句終結了,所以下面是個全新的函數,

不會調用是理所當然。另一方面,不會彈出“2”說明了typeof XMLHttpRequest 不等於"undefined"在兩個瀏覽器都是成立的,說明了兩個瀏覽器都支援XMLHttpRequest()函數!

XMLHttpRequest = function(){

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

return false;

}

return new XMLHttpRequest();

}

 

 

4.改成如下,兩個瀏覽器都是空白的,沒反應。

只要放在catch(e){}之前:

function getHTTPObject(){

if(typeof XMLHttpRequest == "undefined")

XMLHttpRequest = function(){

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

alert("4");    //錯誤原因是缺少 ‘catch‘,不能分開的。

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

return false;

}

return new XMLHttpRequest();

}

或者

function getHTTPObject(){

if(typeof XMLHttpRequest == "undefined")

XMLHttpRequest = function(){

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

return false;

}alert("5");  //如果alert寫在下一行,那麼就是第2點的結果,為什嗎???

           //因為分行符就是等於自動添加分號;。所以正確寫法應該是};alert("5");

return new XMLHttpRequest();

}

 

5.改成如下,或者放在其他上述沒提到的地方。回到了第1點的結果。說明了什嗎?

function getHTTPObject(){

if(typeof XMLHttpRequest == "undefined")

XMLHttpRequest = function(){

alert("5");    //說明這裡有沒有沒差別,可能根本沒運行這裡。結合上面,這個函數typeof XMLHttpRequest == "undefined"是一直不等的,所以這裡肯定不運行,直接return newXMLHttpRequest();說明了一個問題IE中typeof XMLHttpRequest 不等於"undefined",但是系統的 XMLHttpRequest();也無法直接運行,需要伺服器環境。

 

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

return false;

}

return new XMLHttpRequest();

}

 

7.從測試結果來看,Firefox預設擁有自身的伺服器,不需要另外搭建

8.IE中"Response Received"一直先於"function Done",響應比函數快。Firefox則相反。

(測試中發現響應速度與F12文檔模式有關,說明IE預設的edge響應比一般的8快)

 所以可以忽略上面的先後順序。

 

9.改成如下,IE中彈出”1”,"Response Received","function Done",頁面正常

Firefox只彈1,空白頁面。

function getHTTPObject(){

if(typeof XMLHttpRequest == "undefined")

alert("2");                 

XMLHttpRequest = function(){

alert("1");           //說明了當上面有alert函數時這裡一定會被運行到。

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

return false;

}

return new XMLHttpRequest();

}

10.如下,IE只彈出"Response Received","function Done",頁面正常。

但是Firefox只彈1,空白頁面

function getHTTPObject(){

if(typeof XMLHttpRequest == "undefined")

alert("2");                 

XMLHttpRequest = function(){

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

alert("1");    //說明Firefox會運行到這裡,IE不會。

return false;

}

return new XMLHttpRequest();

}

11.總結性的測試。IE中彈出1,5,3,"Response Received","function Done",頁面正常。

Firefox中彈出1,5,3,4,空白頁面。

function getHTTPObject(){

alert("1");

if(typeof XMLHttpRequest == "undefined")

alert("2");                 

XMLHttpRequest = function(){

alert("3");

try{return new ActiveXObject("Msxml2.XMLHTTP.6.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP.3.0");}

catch(e){}

try{return new ActiveXObject("Msxml2.XMLHTTP");}

catch(e){}

alert("4");   //結合上面,一切都明了了,if語句被終結,兩個瀏覽器都支援XMLHttpRequest(),所以進行最後一步return new XMLHttpRequest();,調用自訂的函數(注意這裡的是自訂的XMLHttpRequest()函數,不是系統的,所以IE也能運行了),彈出“3”,然後IE中也支援Msxml2.XMLHTTP.6.0或者其中的一個,所以IE不會進行到彈出“4”這一步,就可以正常地顯示頁面了。Firefox不支援Msxml2.XMLHTTP.6.0或者ActiveXObject(),所以會進行到底,而且沒有正確地顯示頁面。

 

return false;

}

alert("5");

return new XMLHttpRequest();

}

ajax在IE中無法正常測試原因的探索及學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.