• <sub id="h4knl"><ol id="h4knl"></ol></sub>
    <sup id="h4knl"></sup>
      <sub id="h4knl"></sub>

      <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
      1. <strong id="h4knl"></strong>

      2. Ajax的使用四大詳細步驟

        時間:2024-11-06 03:43:48 AJAX 我要投稿
        • 相關推薦

        Ajax的使用四大詳細步驟

          什么是ajax?

          ajax(異步javascript xml) 能夠刷新局部網頁數據而不是重新加載整個網頁。

          如何使用ajax?

          第一步,創建xmlhttprequest對象,var xmlhttp =new XMLHttpRequest();XMLHttpRequest對象用來和服務器交換數據。

          var xhttp;if (window.XMLHttpRequest) {//現代主流瀏覽器xhttp = new XMLHttpRequest();} else {// 針對瀏覽器,比如IE5或IE6xhttp = new ActiveXObject("Microsoft.XMLHTTP");}

          第二步,使用xmlhttprequest對象的open()和send()方法發送資源請求給服務器。

          xmlhttp.open(method,url,async) method包括get 和post,url主要是文件或資源的路徑,async參數為true(代表異步)或者false(代表同步)

          xhttp.send();使用get方法發送請求到服務器。

          xhttp.send(string);使用post方法發送請求到服務器。

          post 發送請求什么時候能夠使用呢?

          (1)更新一個文件或者數據庫的時候。

          (2)發送大量數據到服務器,因為post請求沒有字符限制。

          (3)發送用戶輸入的加密數據。

          get例子:

          xhttp.open("GET", "ajax_info.txt", true);xhttp.open("GET", "index.html", true);xhttp.open("GET", "demo_get.asp" + Math.random(), true);xhttp.send();

          post例子

          xhttp.open("POST", "demo_post.asp", true);xhttp.send();

          post表單數據需要使用xmlhttprequest對象的setRequestHeader方法增加一個HTTP頭。

          post表單例子

          xhttp.open("POST", "ajax_test.aspx", true);xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xhttp.send("fname=Henry&lname=Ford");

          async=true 當服務器準備響應時將執行onreadystatechange函數。

          xhttp.onreadystatechange = function() {if (xhttp.readyState == 4 && xhttp.status == 200) {document.getElementById("demo").innerHTML = xhttp.responseText;}};xhttp.open("GET", "index.aspx", true);xhttp.send();

          asyn=false 則將不需要寫onreadystatechange函數,直接在send后面寫上執行代碼。

          xhttp.open("GET", "index.aspx", false);xhttp.send();document.getElementById("demo").innerHTML = xhttp.responseText;

          第三步,使用xmlhttprequest對象的responseText或responseXML屬性獲得服務器的響應。

          使用responseText屬性得到服務器響應的字符串數據,使用responseXML屬性得到服務器響應的XML數據。

          例子如下:

          document.getElementById("demo").innerHTML = xhttp.responseText;

          服務器響應的XML數據需要使用XML對象進行轉換。

          例子:

          xmlDoc = xhttp.responseXML;txt = "";x = xmlDoc.getElementsByTagName("ARTIST");for (i = 0; i < x.length; i++) {txt += x[i].childNodes[0].nodeValue + "

          ";}document.getElementById("demo").innerHTML = txt;

          第四步,onreadystatechange函數,當發送請求到服務器,我們想要服務器響應執行一些功能就需要使用onreadystatechange函數,每次xmlhttprequest對象的readyState發生改變都會觸發onreadystatechange函數。

          onreadystatechange屬性存儲一個當readyState發生改變時自動被調用的函數。

          readyState屬性,XMLHttpRequest對象的狀態,改變從0到4,0代表請求未被初始化,1代表服務器連接成功,2請求被服務器接收,3處理請求,4請求完成并且響應準備。

          status屬性,200表示成功響應,404表示頁面不存在。

          在onreadystatechange事件中,服務器響應準備的時候發生,當readyState==4和status==200的時候服務器響應準備。

          例子:

          function loadDoc() {var xhttp = new XMLHttpRequest();xhttp.onreadystatechange = function() {if (xhttp.readyState == 4 && xhttp.status == 200) {document.getElementById("demo").innerHTML = xhttp.responseText;}};xhttp.open("GET", "ajax_info.txt", true);xhttp.send();} //函數作為參數調用

          Let AJAX change this text.

          Change Content

          function loadDoc(url, cfunc) {var xhttp;xhttp=new XMLHttpRequest();xhttp.onreadystatechange = function() {if (xhttp.readyState == 4 && xhttp.status == 200) {cfunc(xhttp);}};xhttp.open("GET", url, true);xhttp.send();}function myFunction(xhttp) {document.getElementById("demo").innerHTML = xhttp.responseText;}

        《&.doc》
        将本文的Word文档下载到电脑,方便收藏和打印
        推荐度:
        点击下载文档

        【Ajax的使用四大詳細步驟】相關文章:

        使用Word2010創建圖表的詳細操作步驟07-04

        使用ajax操作JavaScript對象的方法09-28

        美甲的詳細步驟07-11

        做壽司的詳細步驟10-05

        Ajax使用JSON數據格式案例分析01-10

        企業管理的詳細步驟08-13

        咖啡拉花的詳細步驟10-15

        大醬湯的做法詳細步驟08-05

        怎么做壽司詳細步驟09-27

        結婚迎親詳細流程步驟01-01

        在线咨询
        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码
      3. <sub id="h4knl"><ol id="h4knl"></ol></sub>
        <sup id="h4knl"></sup>
          <sub id="h4knl"></sub>

          <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
          1. <strong id="h4knl"></strong>

          2. 日本亚洲五月天在线观看视频 | 熟女精品视频一区 | 亚洲国产日韩在线人高清 | 亚洲Av电影在线观看不卡 | 一区二区三区在线观看视频 | 亚洲人成网999久久久综合 |

            Ajax的使用四大詳細步驟

              什么是ajax?

              ajax(異步javascript xml) 能夠刷新局部網頁數據而不是重新加載整個網頁。

              如何使用ajax?

              第一步,創建xmlhttprequest對象,var xmlhttp =new XMLHttpRequest();XMLHttpRequest對象用來和服務器交換數據。

              var xhttp;if (window.XMLHttpRequest) {//現代主流瀏覽器xhttp = new XMLHttpRequest();} else {// 針對瀏覽器,比如IE5或IE6xhttp = new ActiveXObject("Microsoft.XMLHTTP");}

              第二步,使用xmlhttprequest對象的open()和send()方法發送資源請求給服務器。

              xmlhttp.open(method,url,async) method包括get 和post,url主要是文件或資源的路徑,async參數為true(代表異步)或者false(代表同步)

              xhttp.send();使用get方法發送請求到服務器。

              xhttp.send(string);使用post方法發送請求到服務器。

              post 發送請求什么時候能夠使用呢?

              (1)更新一個文件或者數據庫的時候。

              (2)發送大量數據到服務器,因為post請求沒有字符限制。

              (3)發送用戶輸入的加密數據。

              get例子:

              xhttp.open("GET", "ajax_info.txt", true);xhttp.open("GET", "index.html", true);xhttp.open("GET", "demo_get.asp" + Math.random(), true);xhttp.send();

              post例子

              xhttp.open("POST", "demo_post.asp", true);xhttp.send();

              post表單數據需要使用xmlhttprequest對象的setRequestHeader方法增加一個HTTP頭。

              post表單例子

              xhttp.open("POST", "ajax_test.aspx", true);xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xhttp.send("fname=Henry&lname=Ford");

              async=true 當服務器準備響應時將執行onreadystatechange函數。

              xhttp.onreadystatechange = function() {if (xhttp.readyState == 4 && xhttp.status == 200) {document.getElementById("demo").innerHTML = xhttp.responseText;}};xhttp.open("GET", "index.aspx", true);xhttp.send();

              asyn=false 則將不需要寫onreadystatechange函數,直接在send后面寫上執行代碼。

              xhttp.open("GET", "index.aspx", false);xhttp.send();document.getElementById("demo").innerHTML = xhttp.responseText;

              第三步,使用xmlhttprequest對象的responseText或responseXML屬性獲得服務器的響應。

              使用responseText屬性得到服務器響應的字符串數據,使用responseXML屬性得到服務器響應的XML數據。

              例子如下:

              document.getElementById("demo").innerHTML = xhttp.responseText;

              服務器響應的XML數據需要使用XML對象進行轉換。

              例子:

              xmlDoc = xhttp.responseXML;txt = "";x = xmlDoc.getElementsByTagName("ARTIST");for (i = 0; i < x.length; i++) {txt += x[i].childNodes[0].nodeValue + "

              ";}document.getElementById("demo").innerHTML = txt;

              第四步,onreadystatechange函數,當發送請求到服務器,我們想要服務器響應執行一些功能就需要使用onreadystatechange函數,每次xmlhttprequest對象的readyState發生改變都會觸發onreadystatechange函數。

              onreadystatechange屬性存儲一個當readyState發生改變時自動被調用的函數。

              readyState屬性,XMLHttpRequest對象的狀態,改變從0到4,0代表請求未被初始化,1代表服務器連接成功,2請求被服務器接收,3處理請求,4請求完成并且響應準備。

              status屬性,200表示成功響應,404表示頁面不存在。

              在onreadystatechange事件中,服務器響應準備的時候發生,當readyState==4和status==200的時候服務器響應準備。

              例子:

              function loadDoc() {var xhttp = new XMLHttpRequest();xhttp.onreadystatechange = function() {if (xhttp.readyState == 4 && xhttp.status == 200) {document.getElementById("demo").innerHTML = xhttp.responseText;}};xhttp.open("GET", "ajax_info.txt", true);xhttp.send();} //函數作為參數調用

              Let AJAX change this text.

              Change Content

              function loadDoc(url, cfunc) {var xhttp;xhttp=new XMLHttpRequest();xhttp.onreadystatechange = function() {if (xhttp.readyState == 4 && xhttp.status == 200) {cfunc(xhttp);}};xhttp.open("GET", url, true);xhttp.send();}function myFunction(xhttp) {document.getElementById("demo").innerHTML = xhttp.responseText;}