AJAX 的操作和調用

來源:互聯網
上載者:User

標籤:發送資料   app   ext   頁面   inner   沒有   text   分頁載入   ml2   

AJAX(非同步資料處理): Asynchronous (非同步)的JavaScript  And  XML ( 局部重新整理技術 )

  ajax能做什麼:

    頁面實現的效果是局部重新整理,這種效果都是ajax操作的

    使用者名稱線上監測,搜尋提示等等

    無重新整理分頁,滾動分頁載入,微博非同步資料發布

    ajax聊天室

  ajax優點:

    速度快:ajax引擎緩衝了重複提交的資料

    體驗好

    減少了伺服器和資料庫的讀寫壓力

  ajax資料處理流程:

    伺服器端:  PHP+MYSQL+EMAIL(.net,java....)    m=news

    中間:xmlhttp協議

    瀏覽器端:JavaScript + CSS +HTML

 

ajax資料處理流程:

  JavaScript 得到表單中的資料,將需要提交的資料傳遞給ajax引擎(xmlhttp協議的解析器)ajax引擎會緩衝使用者提交的資料,並且將需要提交的資料進行提交。

  資料提交伺服器,伺服器處理完畢後,會將資料通過xmlhttp協議對象返回到瀏覽器中,ajax引擎會將返回的資料和提交的資料匹配,然後緩衝到ajax引擎中,然後js從ajax引擎中擷取資料.

  資料第二次提交時,如果提交的資料已經提交過,ajax引擎有匹配的返回資料,那麼ajax引擎則不會再向伺服器提交資料,而會從ajax引擎直接將原來緩衝的資料直接返回。

 

ajax操作步驟:

  建立xmlhttp協議對象:

  IE: activeXobject外掛程式 IE8上用的是FF系列的xmlhttprequest對象

  FF: xmlhttprequest對象(類)

var http_request= false;    if(window.XMLHttpRequest) { //FF Chrome 瀏覽器          http_request = new XMLHttpRequest();     }else if (window.ActiveXObject) { // IE瀏覽器  IE9 以下        try {            http_request = new ActiveXObject("Msxml2.XMLHTTP");          } catch (es) {            try {                http_request = new ActiveXObject("Microsoft.XMLHTTP");              } catch (es) {}        }    }IETester

 

  設定監視控制代碼:監控整個的ajax資料處理流程

    readyState: 擷取當前ajax的處理狀態 

      不同的瀏覽器狀態不同,有的是1,4有的是1234

      0  未建立ajax對象

      1  ajax對象建立成功,還沒有準備發送的資料

      2  ajax發送的資料已經準備完畢,還沒有發送

      3  ajax已經發送資料,還沒有接受完資料

      4  ajax接受完畢資料

    readyState的狀態數值,每更改一次,都會觸發一次的onreadystatechange

    ajax對象.onreadystatechange = 方法名;

  準備發送的資料:

    ajax對象.open(“提交的方式”,”提交頁面和資料”,同步/非同步)

    提交方式:  GET/POST

    提交的頁面和資料:

      GET: 比如  index.php?m=news&username=tom

      POST: 提交的頁面

    同步/非同步: true:非同步   false:同步

      同步:資料一次性提交,一次性接收(多個ajax挨個提交接收)

      非同步:資料一邊提交,一邊接收(多個ajax可能一起提交)

  發送資料:

    ajax對象.send([資料]);

    GET: send無參數

    POST: send中放置的時post提交的資料,並且提交的是字串

  監視代碼中:

    發送和接受過程中不能有錯誤

    ajax對象.status = 200  

    200:資料發送和接受無錯誤

  接受資料:

    ajax對象.responseText;     #返回的是伺服器返回的資料#

 

  【user.html】  #使用者名稱線上監測user.html  user.php    user表 pdo操作資料庫# 

<html>    <head>    <script>            // ajax封裝函數        function ajax(method,url,data,asyc,success){            // 建立xmlhttp協議對象            var http_request= false;            if(window.XMLHttpRequest) { //Mozilla 瀏覽器                   http_request = new XMLHttpRequest();            }else if (window.ActiveXObject) { // IE瀏覽器                 try {                       http_request = new ActiveXObject("Msxml2.XMLHTTP");                   } catch (es) {                       try {                           http_request = new ActiveXObject("Microsoft.XMLHTTP");                       } catch (es) {}                   }               }            http_request.onreadystatechange = check;            // GET方式提交            if(method.toUpperCase() == "GET"){                http_request.open(method,url+"?random="+Math.random()+"&"+data,asyc);                http_request.send();            }            // POST方式提交            if(method.toUpperCase() == "POST"){                http_request.open(method,url,asyc);                // 設定xmlhttp請求的頭資訊  變數=值&變數=值                http_request.setRequestHeader("content-Type","application/x-www-form-urlencoded");                http_request.send("random="+Math.random()+"&"+data);            }                        function check(){                if(http_request.readyState == 4 && http_request.status ==200){                    var text = http_request.responseText;                                        if(text == "1"){                        var text = "使用者名稱已存在";                        var color = "red";                    }else{                        var text = "使用者名稱可用";                        var color = "green";                    }                    info.innerHTML = text;                    info.style.color = color;                }            }        }    </script>    </head>    <body>    </body></html>

  【user.php】

<?php echo $_GET[‘uname‘]?>

 

AJAX 的操作和調用

聯繫我們

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