PHP實現簡單ajax Loading載入功能的方法

來源:互聯網
上載者:User
這篇文章主要介紹了PHP實現簡單ajax Loading載入功能的方法,結合執行個體形式分析了ajax載入的原理、操作技巧與相關注意事項,需要的朋友可以參考下

具體如下:

var xmlHttp;function createXmlHttpReq() {  if(window.ActiveXObject) {    xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');  } else if(window.XMLHttpRequest) {    xmlHttp = new XMLHttpRequest();  }}function funMy(url) {  createXmlHttpReq();  try {    xmlHttp.onreadystatechange = cb;//一定要在open()前,下邊會有說明。在此處犯錯了    xmlHttp.open("GET","for.php?id="+url,true);    xmlHttp.send(null);  } catch(e) {    alert("您訪問的資源不存在");  }}//回呼函數function cb() {  if(xmlHttp.readyState==1) {    alert("1-------------->");    //在Google Chrome 瀏覽器裡不顯示loading圖片,三秒後顯示內容,問題已解決,下邊有說明    document.getElementById('ajax').innerHTML = "<img src=loading2.gif>";    //document.getElementById('ajax').innerHTML = "Loading......";  }  if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {      var data = xmlHttp.responseText;      document.getElementById('ajax').innerHTML = data;  }}

測試的時候,被 Chrome 給絆著了。請看下邊解釋:

這樣寫本來就不會接到.readyState==1的回應

因為1是表示已經調用.open()完成

但是.open()在.onreadystatechange事件前就被調用了,所以你應該不可能接到.readyState==1的回應

故,想接到.readyState==1 =>.onreadystatechange必須在.open()之前
那麼為什麼有時候接到呢?

因為你使用同一個全域變數...在連續操作時有可能會因為一個xhr請求還在等候php而又將它再次初始化引發

應該先決定資料的處理方式onreadystatechange,在送出要處理的資料open()

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.