{"id":9178,"date":"2024-05-05T15:01:01","date_gmt":"2024-05-05T07:01:01","guid":{"rendered":""},"modified":"2024-05-05T15:01:01","modified_gmt":"2024-05-05T07:01:01","slug":"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672f","status":"publish","type":"post","link":"https:\/\/mushiming.com\/9178.html","title":{"rendered":"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672f"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

AJAX\u7b80\u4ecb<\/h3>\n

\u4ec0\u4e48\u662fAJAX\uff1f<\/strong><\/p>\n

    \n
  1. \n

    AJAX = Asynchronous JavaScript And XML.<\/p>\n<\/li>\n

  2. \n

    AJAX\u5e76\u975e\u7f16\u7a0b\u8bed\u8a00\uff0c\u662f\u4e00\u79cd\u5b9e\u73b0\u524d\u540e\u7aef\u6570\u636e\u4ea4\u6362\u7684\u6280\u672f<\/p>\n<\/li>\n

  3. \n

    AJAX \u4ec5\u4ec5\u7ec4\u5408\u4e86\uff1a<\/p>\n

     - \u6d4f\u89c8\u5668\u5185\u5efa\u7684 XMLHttpRequest \u5bf9\u8c61\uff08\u4ece web \u670d\u52a1\u5668\u8bf7\u6c42\u6570\u636e\uff09\n - JavaScript \u548c HTML DOM\uff08\u663e\u793a\u6216\u4f7f\u7528\u6570\u636e\uff09\n<\/code><\/pre>\n<\/li>\n<\/ol>\n

    AJAX\u5de5\u4f5c\u6d41\u7a0b<\/strong><\/p>\n

      \n
    1. \u7f51\u9875\u4e2d\u53d1\u751f\u4e00\u4e2a\u4e8b\u4ef6\uff08\u9875\u9762\u52a0\u8f7d\u3001\u6309\u94ae\u70b9\u51fb\uff09<\/li>\n
    2. \u7531 JavaScript \u521b\u5efa XMLHttpRequest \u5bf9\u8c61<\/li>\n
    3. XMLHttpRequest \u5bf9\u8c61\u5411 web \u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42<\/li>\n
    4. \u670d\u52a1\u5668\u5904\u7406\u8be5\u8bf7\u6c42<\/li>\n
    5. \u670d\u52a1\u5668\u5c06\u54cd\u5e94\u53d1\u9001\u56de\u7f51\u9875<\/li>\n
    6. \u7531 JavaScript\u8bfb\u53d6\u54cd\u5e94<\/li>\n
    7. \u7531 JavaScript \u6267\u884c\u6b63\u786e\u7684\u52a8\u4f5c\uff08\u6bd4\u5982\u66f4\u65b0\u9875\u9762\uff09<\/li>\n<\/ol>\n

      \"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672f<\/p>\n

      XMLHttpRequest\u5bf9\u8c61<\/strong><\/p>\n

      AJAX\u7684\u6838\u5fc3\u662fXMLHttpRequest\u5bf9\u8c61\u3002<\/p>\n

      XMLHttpRequest \u5bf9\u8c61\u7528\u4e8e\u540c\u5e55\u540e\u670d\u52a1\u5668\u4ea4\u6362\u6570\u636e\u3002\u8fd9\u610f\u5473\u7740\u53ef\u4ee5\u66f4\u65b0\u7f51\u9875\u7684\u90e8\u5206\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u52a0\u8f7d\u6574\u4e2a\u9875\u9762\u3002<\/p>\n

      XMLHttpRequest\u5bf9\u8c61\u521b\u5efa<\/strong><\/p>\n

      \u6240\u6709\u73b0\u4ee3\u6d4f\u89c8\u5668\uff08Chrom\u3001IE7+\u3001Firefox\u3001Safari \u4ee5\u53ca Opera\uff09\u90fd\u6709\u5185\u5efa\u7684 XMLHttpRequest \u5bf9\u8c61\u3002<\/p>\n

      \u521b\u5efa XMLHttpRequest \u7684\u8bed\u6cd5\u662f\uff1avariable = new XMLHttpRequest();<\/code><\/p>\n

      \u4e3a\u4e86\u5e94\u5bf9\u6240\u6709\u6d4f\u89c8\u5668\uff0c\u5305\u62ec IE5 \u548c IE6\uff0c\u8bf7\u68c0\u67e5\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301 XMLHttpRequest \u5bf9\u8c61\u3002\u5982\u679c\u652f\u6301\uff0c\u521b\u5efa XMLHttpRequest \u5bf9\u8c61\uff0c\u5982\u679c\u4e0d\u652f\u6301\uff0c\u5219\u521b\u5efa ActiveX \u5bf9\u8c61\uff1a<\/p>\n

      var<\/span> xhttp;<\/span>\nif<\/span> (<\/span>window.<\/span>XMLHttpRequest)<\/span> { \n   <\/span>\n    xhttp =<\/span> new<\/span> XMLHttpRequest<\/span>(<\/span>)<\/span>;<\/span>\n    }<\/span> else<\/span> { \n   <\/span>\n    \/\/ code for IE6, IE5<\/span>\n     xhttp =<\/span> new<\/span> ActiveXObject<\/span>(<\/span>\"Microsoft.XMLHTTP\"<\/span>)<\/span>;<\/span>\n    }<\/span>\n}<\/span>\n<\/code><\/pre>\n

      XMLHttpRequestduixang \u5bf9\u8c61\u65b9\u6cd5<\/strong>
      \"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672f<\/p>\n

      XMLHttpRequest\u5bf9\u8c61\u5c5e\u6027<\/strong><\/p>\n

      \"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672f
      XMLHttpRequest \u5bf9\u8c61\u53d1\u9001\u8bf7\u6c42<\/strong>
      \u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42<\/em><\/p>\n

      xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"ajax_info.txt\"<\/span>,<\/span> true<\/span>)<\/span>;<\/span>\nxhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

      \"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672f
      GET \u8fd8\u662f POST\uff1f<\/strong><\/em><\/p>\n

      GET \u6bd4 POST \u66f4\u7b80\u5355\u66f4\u5feb\uff0c\u53ef\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u3002<\/p>\n

      \u4e0d\u8fc7\uff0c\u8bf7\u5728\u4ee5\u4e0b\u60c5\u51b5\u59cb\u7ec8\u4f7f\u7528 POST\uff1a<\/p>\n

        \n
      1. \u7f13\u5b58\u6587\u4ef6\u4e0d\u662f\u9009\u9879\uff08\u66f4\u65b0\u670d\u52a1\u5668\u4e0a\u7684\u6587\u4ef6\u6216\u6570\u636e\u5e93\uff09<\/li>\n
      2. \u5411\u670d\u52a1\u5668\u53d1\u9001\u5927\u91cf\u6570\u636e\uff08POST \u65e0\u5927\u5c0f\u9650\u5236\uff09<\/li>\n
      3. \u53d1\u9001\u7528\u6237\u8f93\u5165\uff08\u53ef\u5305\u542b\u672a\u77e5\u5b57\u7b26\uff09\uff0cPOST\u6bd4 GET \u66f4\u5f3a\u5927\u66f4\u5b89\u5168<\/li>\n<\/ol>\n

        GET\u8bf7\u6c42<\/strong><\/em><\/p>\n

        xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"demo_get.asp?t=\"<\/span> +<\/span> Math.<\/span>random<\/span>(<\/span>)<\/span>,<\/span> true<\/span>)<\/span>;<\/span>\nxhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

        POST\u8bf7\u6c42<\/strong><\/em><\/p>\n

        \u5982\u9700\u50cf HTML \u8868\u5355\u90a3\u6837 POST \u6570\u636e\uff0c\u8bf7\u901a\u8fc7 setRequestHeader() \u6dfb\u52a0\u4e00\u4e2a HTTP \u5934\u90e8\u3002\u8bf7\u5728 send() \u65b9\u6cd5\u4e2d\u89c4\u5b9a\u60a8\u9700\u8981\u53d1\u9001\u7684\u6570\u636e\uff1a<\/p>\n

        xhttp.<\/span>open<\/span>(<\/span>\"POST\"<\/span>,<\/span> \"ajax_test.asp\"<\/span>,<\/span> true<\/span>)<\/span>;<\/span>\nxhttp.<\/span>setRequestHeader<\/span>(<\/span>\"Content-type\"<\/span>,<\/span> \"application\/x-www-form-urlencoded\"<\/span>)<\/span>;<\/span>\nxhttp.<\/span>send<\/span>(<\/span>\"fname=Bill&lname=Gates\"<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

        \"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672f
        url - \u670d\u52a1\u5668\u4e0a\u7684\u6587\u4ef6<\/strong><\/em>
        open() \u65b9\u6cd5\u7684 url \u53c2\u6570\uff0c\u662f\u670d\u52a1\u5668\u4e0a\u6587\u4ef6\u7684\u5730\u5740\uff1a<\/p>\n

        xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"ajax_test.asp\"<\/span>,<\/span> true<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

        \u8be5\u6587\u4ef6\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u5982 .txt \u548c .xml\uff0c\u6216\u670d\u52a1\u5668\u811a\u672c\u6587\u4ef6\uff0c\u5982 .asp \u548c .php\uff08\u5b83\u4eec\u53ef\u4ee5\u5728\u53d1\u9001\u56de\u54cd\u5e94\u4e4b\u524d\u5728\u670d\u52a1\u5668\u6267\u884c\u64cd\u4f5c\uff09\u3002<\/p>\n

        \u5f02\u6b65 - ture \u8fd8\u662f false\uff1f<\/strong><\/em><\/p>\n

        \u5982\u9700\u5f02\u6b65\u53d1\u9001\u8bf7\u6c42\uff0copen() \u65b9\u6cd5\u7684 async \u53c2\u6570\u5fc5\u987b\u8bbe\u7f6e\u4e3a true\uff1a<\/p>\n

        xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"ajax_test.asp\"<\/span>,<\/span> true<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

        \u53d1\u9001\u5f02\u6b65\u8bf7\u6c42\u5bf9 web \u5f00\u53d1\u4eba\u5458\u6765\u8bf4\u662f\u4e00\u4e2a\u5de8\u5927\u7684\u8fdb\u6b65\u3002\u670d\u52a1\u5668\u4e0a\u6267\u884c\u7684\u8bb8\u591a\u4efb\u52a1\u90fd\u975e\u5e38\u8017\u65f6\u3002\u5728 AJAX \u4e4b\u524d\uff0c\u6b64\u64cd\u4f5c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5e94\u7528\u7a0b\u5e8f\u6302\u8d77\u6216\u505c\u6b62\u3002<\/p>\n

        \u901a\u8fc7\u5f02\u6b65\u53d1\u9001\uff0cJavaScript \u4e0d\u5fc5\u7b49\u5f85\u670d\u52a1\u5668\u54cd\u5e94\uff0c\u800c\u662f\u53ef\u4ee5\uff1a<\/p>\n

        \u5728\u7b49\u5f85\u670d\u52a1\u5668\u54cd\u5e94\u65f6\u6267\u884c\u5176\u4ed6\u811a\u672c
        \u5f53\u54cd\u5e94\u5c31\u7eea\u65f6\u5904\u7406\u54cd\u5e94<\/p>\n

        onreadystatechange \u5c5e\u6027<\/strong><\/em><\/p>\n

        \u901a\u8fc7 XMLHttpRequest \u5bf9\u8c61\uff0c\u60a8\u53ef\u4ee5\u5b9a\u4e49\u5f53\u8bf7\u6c42\u63a5\u6536\u5230\u5e94\u7b54\u65f6\u6240\u6267\u884c\u7684\u51fd\u6570\u3002<\/p>\n

        \u8fd9\u4e2a\u51fd\u6570\u662f\u5728 XMLHttpResponse \u5bf9\u8c61\u7684 onreadystatechange \u5c5e\u6027\u4e2d\u5b9a\u4e49\u7684\uff1a<\/p>\n

        \u5b9e\u4f8b
        xhttp.onreadystatechange = function() {
        \n
        if (this.readyState == 4 && this.status == 200) {
        \n
        document.getElementById(\u201cdemo\u201d).innerHTML = this.responseText;
        }
        };
        xhttp.open(\u201cGET\u201d, \u201cajax_info.txt\u201d, true);
        xhttp.send();<\/p>\n

        \u540c\u6b65\u8bf7\u6c42<\/strong><\/em><\/p>\n

        \u5982\u9700\u6267\u884c\u540c\u6b65\u7684\u8bf7\u6c42\uff0c\u8bf7\u628a open() \u65b9\u6cd5\u4e2d\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3a false\uff1a<\/p>\n

        xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"ajax_info.txt\"<\/span>,<\/span> false<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

        \u6709\u65f6 async = false \u7528\u4e8e\u5feb\u901f\u6d4b\u8bd5\u3002\u4f60\u4e5f\u4f1a\u5728\u66f4\u8001\u7684 JavaScript \u4ee3\u7801\u4e2d\u770b\u5230\u540c\u6b65\u8bf7\u6c42\u3002<\/p>\n

        \u7531\u4e8e\u4ee3\u7801\u5c06\u7b49\u5f85\u670d\u52a1\u5668\u5b8c\u6210\uff0c\u6240\u4ee5\u4e0d\u9700\u8981 onreadystatechange \u51fd\u6570\uff1a<\/p>\n

        \u5b9e\u4f8b<\/p>\n

        xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"ajax_info.txt\"<\/span>,<\/span> false<\/span>)<\/span>;<\/span>\nxhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span>\ndocument.<\/span>getElementById<\/span>(<\/span>\"demo\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> xhttp.<\/span>responseText;<\/span>\n<\/code><\/pre>\n

        \u670d\u52a1\u5668\u54cd\u5e94<\/strong><\/p>\n

        onreadystatechange \u5c5e\u6027<\/strong><\/em><\/p>\n

        readyState \u5c5e\u6027\u5b58\u7559 XMLHttpRequest \u7684\u72b6\u6001\u3002<\/p>\n

        onreadystatechange \u5c5e\u6027\u5b9a\u4e49\u5f53 readyState \u53d1\u751f\u53d8\u5316\u65f6\u6267\u884c\u7684\u51fd\u6570\u3002<\/p>\n

        status \u5c5e\u6027\u548c statusText \u5c5e\u6027\u5b58\u6709 XMLHttpRequest \u5bf9\u8c61\u7684\u72b6\u6001\u3002<\/p>\n

        \"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672f
        \u6bcf\u5f53 readyState \u53d1\u751f\u53d8\u5316\u65f6\u5c31\u4f1a\u8c03\u7528 onreadystatechange \u51fd\u6570\u3002<\/p>\n

        \u5f53 readyState \u4e3a 4\uff0cstatus \u4e3a 200 \u65f6\uff0c\u54cd\u5e94\u5c31\u7eea\uff1a<\/p>\n

        function<\/span> loadDoc<\/span>(<\/span>)<\/span> { \n   <\/span>\n    var<\/span> xhttp =<\/span> new<\/span> XMLHttpRequest<\/span>(<\/span>)<\/span>;<\/span>\n    xhttp.<\/span>onreadystatechange<\/span> =<\/span> function<\/span>(<\/span>)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>this<\/span>.<\/span>readyState ==<\/span> 4<\/span> &&<\/span> this<\/span>.<\/span>status ==<\/span> 200<\/span>)<\/span> { \n   <\/span>\n            document.<\/span>getElementById<\/span>(<\/span>\"demo\"<\/span>)<\/span>.<\/span>innerHTML =<\/span>this<\/span>.<\/span>responseText;<\/span>\n       }<\/span>\n    }<\/span>;<\/span>\n    xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"ajax_info.txt\"<\/span>,<\/span> true<\/span>)<\/span>;<\/span>\n    xhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span> \n}<\/span> \n<\/code><\/pre>\n

        \u4f7f\u7528\u56de\u8c03\u51fd\u6570<\/strong><\/em>
        \u56de\u8c03\u51fd\u6570\u662f\u4e00\u79cd\u4f5c\u4e3a\u53c2\u6570\u88ab\u4f20\u9012\u5230\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u51fd\u6570\u3002<\/p>\n

        \u5982\u679c\u60a8\u7684\u7f51\u7ad9\u4e2d\u6709\u591a\u4e2a AJAX \u4efb\u52a1\uff0c\u90a3\u4e48\u60a8\u5e94\u8be5\u521b\u5efa\u4e00\u4e2a\u6267\u884c XMLHttpRequest \u5bf9\u8c61\u7684\u51fd\u6570\uff0c\u4ee5\u53ca\u4e00\u4e2a\u4f9b\u6bcf\u4e2a AJAX \u4efb\u52a1\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n

        \u8be5\u51fd\u6570\u5e94\u5f53\u5305\u542b URL \u4ee5\u53ca\u5f53\u54cd\u5e94\u5c31\u7eea\u65f6\u8c03\u7528\u7684\u51fd\u6570\u3002<\/p>\n

        loadDoc<\/span>(<\/span>\"url-1\"<\/span>,<\/span> myFunction1)<\/span>;<\/span>\n\nloadDoc<\/span>(<\/span>\"url-2\"<\/span>,<\/span> myFunction2)<\/span>;<\/span>\n\nfunction<\/span> loadDoc<\/span>(<\/span>url,<\/span> cFunction)<\/span> { \n   <\/span>\n  var<\/span> xhttp;<\/span>\n  xhttp =<\/span> new<\/span> XMLHttpRequest<\/span>(<\/span>)<\/span>;<\/span>\n  xhttp.<\/span>onreadystatechange<\/span> =<\/span> function<\/span>(<\/span>)<\/span> { \n   <\/span>\n    if<\/span> (<\/span>this<\/span>.<\/span>readyState ==<\/span> 4<\/span> &&<\/span> this<\/span>.<\/span>status ==<\/span> 200<\/span>)<\/span> { \n   <\/span>\n      cFunction<\/span>(<\/span>this<\/span>)<\/span>;<\/span>\n    }<\/span>\n  }<\/span>;<\/span>\n  xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> url,<\/span> true<\/span>)<\/span>;<\/span>\n  xhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>\n\nfunction<\/span> myFunction1<\/span>(<\/span>xhttp)<\/span> { \n   <\/span>\n  \/\/ \u884c\u52a8\u5728\u8fd9\u91cc<\/span>\n }<\/span> \nfunction<\/span> myFunction2<\/span>(<\/span>xhttp)<\/span> { \n   <\/span>\n  \/\/ \u884c\u52a8\u5728\u8fd9\u91cc<\/span>\n }<\/span> \n<\/code><\/pre>\n

        \"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672f
        responseText \u5c5e\u6027<\/strong><\/em><\/p>\n

        responseText \u5c5e\u6027\u4ee5 JavaScript \u5b57\u7b26\u4e32\u7684\u5f62\u5f0f\u8fd4\u56de\u670d\u52a1\u5668\u54cd\u5e94\uff0c\u56e0\u6b64\u60a8\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\u5b83\uff1a<\/p>\n

        document.<\/span>getElementById<\/span>(<\/span>\"demo\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> xhttp.<\/span>responseText;<\/span>\n<\/code><\/pre>\n

        responseXML \u5c5e\u6027<\/strong><\/em><\/p>\n

        XML HttpRequest \u5bf9\u8c61\u6709\u4e00\u4e2a\u5167\u5efa\u7684 XML \u89e3\u6790\u5668\u3002<\/p>\n

        ResponseXML \u5c5e\u6027\u4ee5 XML DOM \u5bf9\u8c61\u8fd4\u56de\u670d\u52a1\u5668\u54cd\u5e94\u3002<\/p>\n

        \u4f7f\u7528\u6b64\u5c5e\u6027\uff0c\u60a8\u53ef\u4ee5\u628a\u54cd\u5e94\u89e3\u6790\u4e3a XML DOM \u5bf9\u8c61\uff1a<\/p>\n

        xmlDoc =<\/span> xhttp.<\/span>responseXML;<\/span>\ntxt =<\/span> \"\"<\/span>;<\/span>\nx =<\/span> xmlDoc.<\/span>getElementsByTagName<\/span>(<\/span>\"ARTIST\"<\/span>)<\/span>;<\/span>\nfor<\/span> (<\/span>i =<\/span> 0<\/span>;<\/span> i <<\/span> x.<\/span>length;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n  txt +=<\/span> x[<\/span>i]<\/span>.<\/span>childNodes[<\/span>0<\/span>]<\/span>.<\/span>nodeValue +<\/span> \"<br>\"<\/span>;<\/span>\n  }<\/span>\ndocument.<\/span>getElementById<\/span>(<\/span>\"demo\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> txt;<\/span>\nxhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span>  \"music_list.xml\"<\/span>,<\/span> true<\/span>)<\/span>;<\/span>\nxhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span>\n<\/code><\/pre>\n

        getAllResponseHeaders() \u65b9\u6cd5<\/strong><\/em><\/p>\n

        getAllResponseHeaders() \u65b9\u6cd5\u8fd4\u56de\u6240\u6709\u6765\u81ea\u670d\u52a1\u5668\u54cd\u5e94\u7684\u5934\u90e8\u4fe1\u606f\u3002<\/p>\n

        var<\/span> xhttp =<\/span> new<\/span> XMLHttpRequest<\/span>(<\/span>)<\/span>;<\/span>\nxhttp.<\/span>onreadystatechange<\/span> =<\/span> function<\/span>(<\/span>)<\/span> { \n   <\/span>\n  if<\/span> (<\/span>this<\/span>.<\/span>readyState ==<\/span> 4<\/span> &&<\/span> this<\/span>.<\/span>status ==<\/span> 200<\/span>)<\/span> { \n   <\/span>\n    document.<\/span>getElementById<\/span>(<\/span>\"demo\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> this<\/span>.<\/span>getAllResponseHeaders<\/span>(<\/span>)<\/span>;<\/span>\n  }<\/span>\n}<\/span>;<\/span>\n<\/code><\/pre>\n

        getResponseHeader() \u65b9\u6cd5<\/strong><\/em><\/p>\n

        getResponseHeader() \u65b9\u6cd5\u8fd4\u56de\u6765\u81ea\u670d\u52a1\u5668\u54cd\u5e94\u7684\u7279\u5b9a\u5934\u90e8\u4fe1\u606f\u3002<\/p>\n

        ```javascript\nvar xhttp = new XMLHttpRequest();\nxhttp.onreadystatechange = function() {\n  if (this.readyState == 4 && this.status == 200) {\n    document.getElementById(\"demo\").innerHTML = this.getResponseHeader(\"Last-Modified\");\n  }\n};\nxhttp.open(\"GET\", \"ajax_info.txt\", true);\nxhttp.send(); \n<\/code><\/pre>\n

        AJAX XML\u5b9e\u4f8b<\/strong><\/p>\n

        \u4f8b\u5b50\u89e3\u91ca
        \u5f53\u7528\u6237\u70b9\u51fb\u201c\u83b7\u53d6 CD \u4fe1\u606f\u201d\u6309\u94ae\u65f6\uff0c\u6267\u884c loadDoc() \u51fd\u6570\u3002<\/p>\n

        loadDoc() \u51fd\u6570\u521b\u5efa XMLHttpRequest \u5bf9\u8c61\uff0c\u6dfb\u52a0\u5f53\u670d\u52a1\u5668\u54cd\u5e94\u5c31\u7eea\u65f6\u6267\u884c\u7684\u51fd\u6570\uff0c\u5e76\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n

        \u5f53\u670d\u52a1\u5668\u54cd\u5e94\u5c31\u7eea\u540e\uff0c\u6784\u5efa HTML \u8868\u683c\uff0c\u4ece XML \u6587\u4ef6\u63d0\u53d6\u8282\u70b9\uff08\u56e0\u7d20\uff09\uff0c\u6700\u540e\u4f7f\u7528\u7531 XML \u6570\u636e\u586b\u5145\u7684 HTML \u8868\u683c\u5bf9\u5143\u7d20\u201cdemo\u201d\u8fdb\u884c\u66f4\u65b0\uff1a<\/p>\n

        function<\/span> loadDoc<\/span>(<\/span>)<\/span> { \n   <\/span>\n  var<\/span> xhttp =<\/span> new<\/span> XMLHttpRequest<\/span>(<\/span>)<\/span>;<\/span>\n   xhttp.<\/span>onreadystatechange<\/span> =<\/span> function<\/span>(<\/span>)<\/span> { \n   <\/span>\n    if<\/span> (<\/span>this<\/span>.<\/span>readyState  ==<\/span> 4<\/span> &&<\/span> this<\/span>.<\/span>status ==<\/span> 200<\/span>)<\/span> { \n   <\/span>\n    myFunction<\/span>(<\/span>this<\/span>)<\/span>;<\/span>\n     }<\/span>\n  }<\/span>;<\/span>\n  xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"music_list.xml\"<\/span>,<\/span> true<\/span>)<\/span>;<\/span>\n  xhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span>\nfunction<\/span> myFunction<\/span>(<\/span>xml)<\/span> { \n   <\/span>\n  var<\/span> i;<\/span>\n  var<\/span> xmlDoc =<\/span> xml.<\/span>responseXML;<\/span>\n  var<\/span> table=<\/span>\"<tr><th>\u827a\u672f\u5bb6<\/th><th>\u66f2\u76ee<\/th><\/tr>\"<\/span>;<\/span>\n  var<\/span> x =<\/span> xmlDoc.<\/span>getElementsByTagName<\/span>(<\/span>\"TRACK\"<\/span>)<\/span>;<\/span>\n  for<\/span> (<\/span>i =<\/span> 0<\/span>;<\/span> i <<\/span>x.<\/span>length;<\/span>  i++<\/span>)<\/span> { \n   <\/span> \n    table +=<\/span> \"<tr><td>\"<\/span> +<\/span>\n    x[<\/span>i]<\/span>.<\/span>getElementsByTagName<\/span>(<\/span>\"ARTIST\"<\/span>)<\/span>[<\/span>0<\/span>]<\/span>.<\/span>childNodes[<\/span>0<\/span>]<\/span>.<\/span>nodeValue  +<\/span>\n    \"<\/td><td>\"<\/span> +<\/span>\n    x[<\/span>i]<\/span>.<\/span>getElementsByTagName<\/span>(<\/span>\"TITLE\"<\/span>)<\/span>[<\/span>0<\/span>]<\/span>.<\/span>childNodes[<\/span>0<\/span>]<\/span>.<\/span>nodeValue  +<\/span>\n    \"<\/td><\/tr>\"<\/span>;<\/span>\n  }<\/span>\n   document.<\/span>getElementById<\/span>(<\/span>\"demo\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> table;<\/span>\n}<\/span> \n<\/code><\/pre>\n

        AJAX PHP\u5b9e\u4f8b<\/p>\n

        \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\uff1a\u5f53\u7528\u6237\u5728\u8f93\u5165\u5b57\u6bb5\u4e2d\u952e\u5165\u5b57\u7b26\u65f6\uff0c\u7f51\u9875\u5982\u4f55\u4e0e web \u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1\uff1a<\/p>\n

        \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7528\u6237\u5728\u8f93\u5165\u5b57\u6bb5\u4e2d\u952e\u5165\u5b57\u7b26\u65f6\uff0c\u4f1a\u6267\u884c\u540d\u4e3a \u201cshowHint()\u201d \u7684\u51fd\u6570\u3002<\/p>\n

        \u6b64\u51fd\u6570\u88ab onkeyup \u4e8b\u4ef6\u89e6\u53d1\u3002<\/p>\n

        <<\/span>html><\/span>\n<<\/span>head><\/span>\n<<\/span>script><\/span>\nfunction<\/span> showHint<\/span>(<\/span>str)<\/span> { \n   <\/span>\n    if<\/span> (<\/span>str.<\/span>length ==<\/span> 0<\/span>)<\/span> { \n   <\/span> \n        document.<\/span>getElementById<\/span>(<\/span>\"txtHint\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> \"\"<\/span>;<\/span>\n        return<\/span>;<\/span>\n    }<\/span> else<\/span> { \n   <\/span>\n        var<\/span> xmlhttp =<\/span> new<\/span> XMLHttpRequest<\/span>(<\/span>)<\/span>;<\/span>\n        xmlhttp.<\/span>onreadystatechange<\/span> =<\/span> function<\/span>(<\/span>)<\/span> { \n   <\/span>\n            if<\/span> (<\/span>this<\/span>.<\/span>readyState ==<\/span> 4<\/span> &&<\/span> this<\/span>.<\/span>status ==<\/span> 200<\/span>)<\/span> { \n   <\/span>\n                document.<\/span>getElementById<\/span>(<\/span>\"txtHint\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> this<\/span>.<\/span>responseText;<\/span>\n            }<\/span>\n        }<\/span>;<\/span>\n        xmlhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"gethint.php?q=\"<\/span> +<\/span> class<\/span>=<\/span>\"marked\"<\/span>><\/span>str,<\/span> true<\/span>)<\/span>;<\/span>\n        xmlhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span>\n    }<\/span>\n}<\/span>\n<<\/span>\/<\/span>script><\/span>\n<<\/span>\/<\/span>head><\/span>\n<<\/span>body><\/span>\n\n<<\/span>p><\/span><<\/span>b><\/span>\u8bf7\u5728\u4e0b\u9762\u7684\u8f93\u5165\u5b57\u6bb5\u4e2d\u952e\u5165\u59d3\u540d\uff1a<<\/span>\/<\/span>b><\/span><<\/span>\/<\/span>p><\/span>\n<<\/span>form><\/span> \n\u59d3\u6216\u540d\uff1a<<\/span>input type=<\/span>\"text\"<\/span> onkeyup=<\/span>\"showHint(this.value)\"<\/span>><\/span>\n<<\/span>\/<\/span>form><\/span>\n<<\/span>p><\/span>\u5efa\u8bae\uff1a<<\/span>span id=<\/span>\"txtHint\"<\/span>><\/span><<\/span>\/<\/span>span><\/span><<\/span>\/<\/span>p><\/span>\n<<\/span>\/<\/span>body><\/span>\n<<\/span>\/<\/span>html><\/span>\n<\/code><\/pre>\n

        \u9996\u5148\uff0c\u68c0\u67e5\u8f93\u5165\u5b57\u6bb5\u662f\u5426\u4e3a\u7a7a\uff08str.length == 0\uff09\uff0c\u5982\u679c\u662f\uff0c\u6e05\u7a7a txtHint \u5360\u4f4d\u7b26\u7684\u5185\u5bb9\u5e76\u9000\u51fa\u51fd\u6570\u3002<\/p>\n

        \u4e0d\u8fc7\uff0c\u5982\u679c\u8f93\u5165\u5b57\u6bb5\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8fdb\u884c\u5982\u4e0b\uff1a<\/p>\n

        \u521b\u5efa XMLHttpRequest \u5bf9\u8c61
        \u521b\u5efa\u5f53\u670d\u52a1\u5668\u54cd\u5e94\u5c31\u7eea\u65f6\u6267\u884c\u7684\u51fd\u6570
        \u53d1\u9001\u8bf7\u6c42\u5230\u670d\u52a1\u5668\u4e0a\u7684 PHP \u6587\u4ef6\uff08gethint.php\uff09
        \u8bf7\u6ce8\u610f\u6dfb\u52a0\u5230 gethint.php \u7684 q \u53c2\u6570
        str \u53d8\u91cf\u4fdd\u5b58\u4e86\u8f93\u5165\u5b57\u6bb5\u7684\u5185\u5bb9<\/p>\n

        PHP \u6587\u4ef6 - \"gethint.php\"<\/strong><\/em>
        \u8fd9\u4e2a PHP \u6587\u4ef6\u68c0\u67e5\u59d3\u540d\u6570\u7ec4\uff0c\u7136\u540e\u5411\u6d4f\u89c8\u5668\u8fd4\u56de\u5bf9\u5e94\u7684\u59d3\u540d\uff1a<\/p>\n

        <?php<\/span>\n\/\/ \u59d3\u540d\u6570\u7ec4<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Ava\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Brielle\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Caroline\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Diana\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Elise\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Fiona\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Grace\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Hannah\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Ileana\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Jane\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Kathryn\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Laura\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Millie\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Nancy\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Opal\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Petty\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Queenie\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Rose\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Shirley\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Tiffany\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Ursula\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Victoria\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Wendy\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Xenia\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Yvette\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Zoe\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Angell\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Adele\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Beatty\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Carlton\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Elisabeth\"<\/span>;<\/span>\n $a<\/span>[<\/span>]<\/span> =<\/span> \"Violet\"<\/span>;<\/span>\n\n\/\/ \u4ece URL \u83b7\u53d6 q \u53c2\u6570<\/span>\n$q<\/span> =<\/span> $_REQUEST<\/span>[<\/span>\"q\"<\/span>]<\/span>;<\/span>\n\n$hint<\/span> =<\/span> \"\"<\/span>;<\/span>\n\n\/\/ \u67e5\u770b\u6570\u7ec4\u4e2d\u6240\u6709 hint\uff0c$q \u662f\u5426\u4e0e \"\" \u76f8\u540c<\/span>\nif<\/span> (<\/span>$q<\/span> !==<\/span> \"\"<\/span>)<\/span> { \n   <\/span>\n    $q<\/span> =<\/span> strtolower<\/span>(<\/span>$q<\/span>)<\/span>;<\/span>\n    $len<\/span>=<\/span>strlen<\/span>(<\/span>$q<\/span>)<\/span>;<\/span>\n    foreach<\/span>(<\/span>$a<\/span> as<\/span> $name<\/span>)<\/span> { \n   <\/span>\n        if<\/span> (<\/span>stristr<\/span>(<\/span>$q<\/span>,<\/span> substr<\/span>(<\/span>$name<\/span>,<\/span> 0<\/span>,<\/span> $len<\/span>)<\/span>)<\/span>)<\/span> { \n   <\/span>\n            if<\/span> (<\/span>$hint<\/span> ===<\/span> \"\"<\/span>)<\/span> { \n   <\/span>\n                $hint<\/span> =<\/span> $name<\/span>;<\/span>\n            }<\/span> else<\/span> { \n   <\/span>\n                $hint<\/span> .<\/span>=<\/span> \", $name<\/span><\/span>\"<\/span>;<\/span>\n            }<\/span>\n         }<\/span>\n    }<\/span>\n}<\/span>\n\n\/\/ \u8f93\u51fa \"no suggestion\"\uff0c\u5982\u679c\u672a\u627e\u5230 hint \u6216\u8f93\u51fa\u6b63\u786e\u7684\u503c<\/span>\n  echo<\/span> $hint<\/span> ===<\/span> \"\"<\/span> ?<\/span> \"no suggestion\"<\/span> :<\/span> $hint<\/span>;<\/span>\n?><\/span>\n<\/code><\/pre>\n

        AJAX ASP \u5b9e\u4f8b<\/strong><\/em><\/p>\n

        \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\uff1a\u5f53\u7528\u6237\u5728\u8f93\u5165\u5b57\u6bb5\u4e2d\u952e\u5165\u5b57\u7b26\u65f6\uff0c\u7f51\u9875\u5982\u4f55\u4e0e web \u670d\u52a1\u5668\u8fdb\u884c\u901a\u4fe1<\/p>\n

        \u4f8b\u5b50\u89e3\u91ca<\/p>\n

        \u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u7528\u6237\u5728\u8f93\u5165\u5b57\u6bb5\u4e2d\u952e\u5165\u5b57\u7b26\u65f6\uff0c\u4f1a\u6267\u884c\u540d\u4e3a \u201cshowHint()\u201d \u7684\u51fd\u6570\u3002<\/p>\n

        \u6b64\u51fd\u6570\u88ab onkeyup \u4e8b\u4ef6\u89e6\u53d1\u3002<\/p>\n

        \u4e0b\u9762\u662f HTML \u4ee3\u7801\uff1a<\/p>\n

        <<\/span>html<\/span>><\/span><\/span>\n<<\/span>head<\/span>><\/span><\/span>\n<<\/span>script<\/span>><\/span><\/span> function<\/span> showHint<\/span>(<\/span>str)<\/span> { \n    <\/span> if<\/span> (<\/span>str.<\/span>length ==<\/span> 0<\/span>)<\/span> { \n    <\/span> document.<\/span>getElementById<\/span>(<\/span>\"txtHint\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> \"\"<\/span>;<\/span> return<\/span>;<\/span> }<\/span> else<\/span> { \n    <\/span> var<\/span> xmlhttp =<\/span> new<\/span> XMLHttpRequest<\/span>(<\/span>)<\/span>;<\/span> xmlhttp.<\/span>onreadystatechange<\/span> =<\/span> function<\/span>(<\/span>)<\/span> { \n    <\/span> if<\/span> (<\/span>this<\/span>.<\/span>readyState ==<\/span> 4<\/span> &&<\/span> this<\/span>.<\/span>status ==<\/span> 200<\/span>)<\/span> { \n    <\/span> document.<\/span>getElementById<\/span>(<\/span>\"txtHint\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> this<\/span>.<\/span>responseText;<\/span> }<\/span> }<\/span>;<\/span> xmlhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span> \"gethint.asp?q=\"<\/span> +<\/span> class<\/span>=<\/span>\"marked\"<\/span>><\/span>str,<\/span> true<\/span>)<\/span>;<\/span> xmlhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span> }<\/span> }<\/span> <\/span><\/<\/span>script<\/span>><\/span><\/span>\n<\/<\/span>head<\/span>><\/span><\/span>\n<<\/span>body<\/span>><\/span><\/span>\n\n<<\/span>p<\/span>><\/span><\/span><<\/span>b<\/span>><\/span><\/span>\u8bf7\u5728\u4e0b\u9762\u7684\u8f93\u5165\u5b57\u6bb5\u4e2d\u952e\u5165\u5b57\u6bcd A-Z\uff1a<\/<\/span>b<\/span>><\/span><\/span><\/<\/span>p<\/span>><\/span><\/span>\n<<\/span>form<\/span>><\/span><\/span> \n\u59d3\u540d\uff1a<<\/span>input<\/span> type<\/span>=<\/span>\"<\/span>text\"<\/span><\/span> onkeyup<\/span>=<\/span>\"<\/span>showHint(this.value)\"<\/span><\/span>><\/span><\/span>\n<\/<\/span>form<\/span>><\/span><\/span>\n<<\/span>p<\/span>><\/span><\/span>\u641c\u7d22\u5efa\u8bae\uff1a<<\/span>span<\/span> id<\/span>=<\/span>\"<\/span>txtHint\"<\/span><\/span>><\/span><\/span><\/<\/span>span<\/span>><\/span><\/span><\/<\/span>p<\/span>><\/span><\/span>\n<\/<\/span>body<\/span>><\/span><\/span>\n<\/code><\/pre>\n

        \u4ee3\u7801\u89e3\u91ca\uff1a
        \u9996\u5148\uff0c\u68c0\u67e5\u8f93\u5165\u5b57\u6bb5\u662f\u5426\u4e3a\u7a7a\uff08str.length == 0\uff09\uff1b\u5982\u679c\u662f\uff0c\u6e05\u7a7a txtHint \u5360\u4f4d\u7b26\u7684\u5185\u5bb9\u5e76\u9000\u51fa\u51fd\u6570\u3002<\/p>\n

        \u4e0d\u8fc7\uff0c\u5982\u679c\u8f93\u5165\u5b57\u6bb5\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8fdb\u884c\u5982\u4e0b\uff1a<\/p>\n

        \u521b\u5efa XMLHttpRequest \u5bf9\u8c61
        \u521b\u5efa\u5f53\u670d\u52a1\u5668\u54cd\u5e94\u5c31\u7eea\u65f6\u6267\u884c\u7684\u51fd\u6570
        \u53d1\u9001\u8bf7\u6c42\u5230\u670d\u52a1\u5668\u4e0a\u7684 ASP \u6587\u4ef6\uff08gethint.asp\uff09
        \u8bf7\u6ce8\u610f\u6dfb\u52a0\u5230 gethint.asp \u7684 q \u53c2\u6570
        str \u53d8\u91cf\u4fdd\u5b58\u4e86\u8f93\u5165\u5b57\u6bb5\u7684\u5185\u5bb9<\/p>\n

        <%\n response.expires=-1\n dim a(32)\n '\u7528\u59d3\u540d\u586b\u5145\u6570\u7ec4\n a(1)=\"Ava\"\n a(2)=\"Brielle\"\n a(3)=\"Caroline\"\n a(4)=\"Diana\"\n a(5)=\"Elise\"\n a(6)=\"Fiona\"\n a(7)=\"Grace\"\n a(8)=\"Hannah\"\n a(9)=\"Ileana\"\n a(10)=\"Jane\"\n a(11)=\"Kathryn\"\n a(12)=\"Laura\"\n a(13)=\"Millie\"\n a(14)=\"Nancy\"\n a(15)=\"Opal\"\n a(16)=\"Petty\"\n a(17)=\"Queenie\"\n a(18)=\"Rose\"\n a(19)=\"Shirley\"\n a(20)=\"Tiffany\"\n a(21)=\"Ursula\"\n a(22)=\"Victoria\"\n a(23)=\"Wendy\"\n a(24)=\"Xenia\"\n a(25)=\"Yvette\"\n a(26)=\"Zoe\"\n a(27)=\"Angell\"\n a(28)=\"Adele\"\n a(29)=\"Beatty\"\n a(30)=\"Carlton\"\n a(31)=\"Elisabeth\"\n a(32)=\"Violet\"\n\n '\u4ece URL \u83b7\u53d6 q \u53c2\u6570\n q=ucase(request.querystring(\"q\"))\n\n '\u67e5\u770b\u6570\u7ec4\u4e2d\u6240\u6709 hint\uff0cq \u7684\u957f\u5ea6\u662f\u5426\u5927\u4e8e 0\n if len(q)>0 then\n   hint=\"\"\n   for i=1 to 30\n     if q=ucase(mid(a(i),1,len(q))) then\n       if hint=\"\" then\n         hint=a(i)\n       else\n         hint=hint & \" , \" & a(i)\n       end if\n     end if\n   next\n end if\n\n '\u5982\u679c\u672a\u627e\u5230 hint\uff0c\u8f93\u51fa \"no suggestion\"\uff0c\u6216\u8f93\u51fa\u6b63\u786e\u7684\u503c\n if hint=\"\" then\n   response.write(\"no suggestion\")\n else\n   response.write(hint)\n end if\n%>\n<\/code><\/pre>\n

        AJAX Database \u5b9e\u4f8b<\/strong><\/em>
        \u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\uff1a\u7f51\u9875\u5982\u4f55\u901a\u8fc7 AJAX \u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u4fe1\u606f\uff1a<\/p>\n

        \u4f8b\u5b50\u89e3\u91ca - showCustomer() \u51fd\u6570
        \u5f53\u7528\u6237\u5728\u4e0a\u9762\u7684\u4e0b\u62c9\u5217\u8868\u4e2d\u9009\u62e9\u4e00\u4f4d\u5ba2\u6237\u540e\uff0c\u6267\u884c\u540d\u4e3a \u201cshowCustomer()\u201d \u51fd\u6570\u3002\u6b64\u51fd\u6570\u88ab onchange \u4e8b\u4ef6\u89e6\u53d1\uff1a<\/p>\n

        function<\/span> showCustomer<\/span>(<\/span>str)<\/span> { \n   <\/span>\n  var<\/span> xhttp;<\/span> \n  if<\/span> (<\/span>str ==<\/span> \"\"<\/span>)<\/span> { \n   <\/span>\n    document.<\/span>getElementById<\/span>(<\/span>\"txtHint\"<\/span>)<\/span>.<\/span>innerHTML =<\/span> \"\"<\/span>;<\/span>\n    return<\/span>;<\/span>\n  }<\/span>\n  xhttp =<\/span> new<\/span> XMLHttpRequest<\/span>(<\/span>)<\/span>;<\/span>\n  xhttp.<\/span>onreadystatechange<\/span> =<\/span> function<\/span>(<\/span>)<\/span> { \n   <\/span>\n    if<\/span> (<\/span>this<\/span>.<\/span>readyState  ==<\/span> 4<\/span> &&<\/span> this<\/span>.<\/span>status ==<\/span> 200<\/span>)<\/span> { \n   <\/span>\n    document.<\/span>getElementById<\/span>(<\/span>\"txtHint\"<\/span>)<\/span>.<\/span>innerHTML  =<\/span> this<\/span>.<\/span>responseText;<\/span>\n    }<\/span>\n  }<\/span>;<\/span>\n  xhttp.<\/span>open<\/span>(<\/span>\"GET\"<\/span>,<\/span>  \"getcustomer.asp?q=\"<\/span> +<\/span> str,<\/span> true<\/span>)<\/span>;<\/span>\n  xhttp.<\/span>send<\/span>(<\/span>)<\/span>;<\/span>\n}<\/span> \n<\/code><\/pre>\n

        showCustomer() \u51fd\u6570\u8fdb\u884c\u5982\u4e0b\uff1a<\/p>\n

        \u68c0\u67e5\u662f\u5426\u9009\u53d6\u5ba2\u6237
        \u521b\u5efa XMLHttpRequest \u5bf9\u8c61
        \u521b\u5efa\u5f53\u670d\u52a1\u5668\u54cd\u5e94\u5c31\u7eea\u65f6\u6267\u884c\u7684\u51fd\u6570
        \u5411\u670d\u52a1\u5668\u4e0a\u7684\u6587\u4ef6\u53d1\u9001\u8bf7\u6c42
        \u8bf7\u6ce8\u610f\uff0c\u53c2\u6570 q \u88ab\u6dfb\u52a0\u5230 URL\uff08\u5e26\u6709\u4e0b\u62c9\u5217\u8868\u7684\u5185\u5bb9\uff09<\/p>\n

        AJAX \u670d\u52a1\u5668\u9875\u9762<\/em><\/p>\n

        \u88ab\u4ee5\u4e0a JavaScript \u8c03\u7528\u7684\u670d\u52a1\u5668\u9875\u9762\u662f\u540d\u4e3a \u201cgetcustomer.asp\u201d \u7684 ASP \u6587\u4ef6\u3002<\/p>\n

        \u4f7f\u7528 PHP \u6216\u5176\u4ed6\u670d\u52a1\u5668\u8bed\u8a00\u80fd\u591f\u8f7b\u677e\u91cd\u5199\u8be5\u670d\u52a1\u5668\u6587\u4ef6\u3002<\/p>\n

        \u8bf7\u53c2\u89c1\u5bf9\u5e94\u7684 PHP \u5b9e\u4f8b\u3002<\/p>\n

        \u201cgetcustomer.asp\u201d \u4e2d\u7684\u6e90\u4ee3\u7801\u4e2d\u8fd0\u884c\u9762\u5411\u6570\u636e\u5e93\u7684\u67e5\u8be2\uff0c\u5e76\u5728 HTML \u8868\u683c\u4e2d\u8fd4\u56de\u7ed3\u679c\uff1a<\/p>\n

        <<\/span>%<\/span>\nresponse.<\/span>expires=<\/span>-<\/span>1<\/span>\nsql=<\/span>\"SELECT * FROM CUSTOMERS WHERE CUSTOMERID=\"<\/span>\nsql=<\/span>sql &<\/span> \"'\"<\/span> &<\/span> request.<\/span>querystring<\/span>(<\/span>\"q\"<\/span>)<\/span> &<\/span> \"'\"<\/span>\n\nset conn=<\/span>Server.<\/span>CreateObject<\/span>(<\/span>\"ADODB.Connection\"<\/span>)<\/span>\nconn.<\/span>Provider=<\/span>\"Microsoft.Jet.OLEDB.4.0\"<\/span>\nconn.<\/span>Open<\/span>(<\/span>Server.<\/span>Mappath<\/span>(<\/span>\"customers.mdb\"<\/span>)<\/span>)<\/span>\nset rs=<\/span>Server.<\/span>CreateObject<\/span>(<\/span>\"ADODB.recordset\"<\/span>)<\/span>\nrs.<\/span>Open sql,<\/span>conn\n\nresponse.<\/span>write<\/span>(<\/span>\"<table>\"<\/span>)<\/span>\ndo<\/span> until rs.<\/span>EOF\n for<\/span> each x in rs.<\/span>Fields\n   response.<\/span>write<\/span>(<\/span>\"<tr><td><b>\"<\/span> &<\/span> x.<\/span>name &<\/span> \"<\/b><\/td>\"<\/span>)<\/span>\n   response.<\/span>write<\/span>(<\/span>\"<td>\"<\/span> &<\/span> x.<\/span>value &<\/span> \"<\/td><\/tr>\"<\/span>)<\/span>\n next\n rs.<\/span>MoveNext\nloop\nresponse.<\/span>write<\/span>(<\/span>\"<\/table>\"<\/span>)<\/span>\n%<\/span>><\/span>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"AJAX\u2014\u2014JavaScript\u524d\u540e\u53f0\u4ea4\u4e92\u6280\u672fAJAX\u7b80\u4ecb\u4ec0\u4e48\u662fAJAX\uff1fAJAX=AsynchronousJavaScriptAndXML.AJAX\u5e76\u975e\u7f16\u7a0b\u8bed\u8a00\uff0c\u662f\u4e00\u79cd\u5b9e\u73b0\u524d\u540e...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9178"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=9178"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9178\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=9178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=9178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=9178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}