{"id":183404,"date":"2024-11-10T04:32:19","date_gmt":"2024-11-09T20:32:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183404\/"},"modified":"2024-11-10T04:32:19","modified_gmt":"2024-11-09T20:32:19","slug":"%e5%af%a6%e7%8f%be%e7%b6%b2%e9%a0%81%e7%84%a1%e5%88%b7%e6%96%b0%e6%9b%b4%e6%96%b0%ef%bc%8cajax%e5%8b%95%e6%85%8b%e5%8a%a0%e8%bc%89%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a-ajax%e7%9a%84%e5%8b%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183404\/","title":{"rendered":"\u5be6\u73fe\u7db2\u9801\u7121\u5237\u65b0\u66f4\u65b0\uff0cAjax\u52d5\u614b\u52a0\u8f09\u6578\u64da\u5eab\u6578\u64da (ajax\u7684\u52d5\u614b\u52a0\u8f09\u6578\u64da\u5eab\u6578\u64da)"},"content":{"rendered":"<h1 id=\"%e5%af%a6%e7%8f%be%e7%b6%b2%e9%a0%81%e7%84%a1%e5%88%b7%e6%96%b0%e6%9b%b4%e6%96%b0%ef%bc%8cajax%e5%8b%95%e6%85%8b%e5%8a%a0%e8%bc%89%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a-rBOjjLFhtl\">\u5be6\u73fe\u7db2\u9801\u7121\u5237\u65b0\u66f4\u65b0\uff0cAjax\u52d5\u614b\u52a0\u8f09\u6578\u64da\u5eab\u6578\u64da<\/h1>\n<p>\u5728\u73fe\u4ee3\u7db2\u9801\u958b\u767c\u4e2d\uff0c\u4f7f\u7528\u8005\u9ad4\u9a57\u662f\u8a2d\u8a08\u548c\u958b\u767c\u7684\u6838\u5fc3\u8981\u7d20\u4e4b\u4e00\u3002\u96a8\u8457\u6280\u8853\u7684\u9032\u6b65\uff0c\u7121\u5237\u65b0\u66f4\u65b0\u7684\u9700\u6c42\u8d8a\u4f86\u8d8a\u53d7\u5230\u91cd\u8996\u3002Ajax\uff08Asynchronous JavaScript and XML\uff09\u6280\u8853\u7684\u51fa\u73fe\uff0c\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5728\u4e0d\u91cd\u65b0\u52a0\u8f09\u6574\u500b\u9801\u9762\u7684\u60c5\u6cc1\u4e0b\uff0c\u52d5\u614b\u52a0\u8f09\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u3002\u672c\u6587\u5c07\u63a2\u8a0eAjax\u7684\u57fa\u672c\u6982\u5ff5\u3001\u5be6\u73fe\u65b9\u6cd5\u4ee5\u53ca\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u7684\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afajax%ef%bc%9f-rBOjjLFhtl\">\u4ec0\u9ebc\u662fAjax\uff1f<\/h2>\n<p>Ajax\u662f\u4e00\u7a2e\u7528\u65bc\u5275\u5efa\u5feb\u901f\u548c\u52d5\u614b\u7db2\u9801\u61c9\u7528\u7684\u6280\u8853\u3002\u5b83\u5141\u8a31\u7db2\u9801\u5728\u4e0d\u91cd\u65b0\u52a0\u8f09\u6574\u500b\u9801\u9762\u7684\u60c5\u6cc1\u4e0b\uff0c\u8207\u4f3a\u670d\u5668\u9032\u884c\u7570\u6b65\u901a\u4fe1\u3002\u9019\u610f\u5473\u8457\u7528\u6236\u53ef\u4ee5\u5728\u9801\u9762\u4e0a\u9032\u884c\u64cd\u4f5c\uff0c\u800c\u4e0d\u5fc5\u7b49\u5f85\u6574\u500b\u9801\u9762\u91cd\u65b0\u52a0\u8f09\uff0c\u5f9e\u800c\u63d0\u9ad8\u4e86\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<h2 id=\"ajax%e7%9a%84%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86-rBOjjLFhtl\">Ajax\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<p>Ajax\u7684\u5de5\u4f5c\u539f\u7406\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<ol>\n<li>\u7528\u6236\u5728\u7db2\u9801\u4e0a\u89f8\u767c\u4e8b\u4ef6\uff08\u4f8b\u5982\u9ede\u64ca\u6309\u9215\uff09\u3002<\/li>\n<li>JavaScript\u5275\u5efa\u4e00\u500bXMLHttpRequest\u5c0d\u8c61\u3002<\/li>\n<li>\u8a72\u5c0d\u8c61\u5411\u4f3a\u670d\u5668\u767c\u9001\u8acb\u6c42\uff0c\u4e26\u6307\u5b9a\u6240\u9700\u7684\u6578\u64da\u3002<\/li>\n<li>\u4f3a\u670d\u5668\u8655\u7406\u8acb\u6c42\u4e26\u8fd4\u56de\u6578\u64da\uff08\u901a\u5e38\u662fJSON\u683c\u5f0f\uff09\u3002<\/li>\n<li>JavaScript\u63a5\u6536\u6578\u64da\u4e26\u66f4\u65b0\u7db2\u9801\u5167\u5bb9\uff0c\u800c\u7121\u9700\u91cd\u65b0\u52a0\u8f09\u6574\u500b\u9801\u9762\u3002<\/li>\n<\/ol>\n<h2 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8ajax%e5%8b%95%e6%85%8b%e5%8a%a0%e8%bc%89%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a-rBOjjLFhtl\">\u5982\u4f55\u4f7f\u7528Ajax\u52d5\u614b\u52a0\u8f09\u6578\u64da\u5eab\u6578\u64da<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Ajax\u5f9e\u6578\u64da\u5eab\u4e2d\u52d5\u614b\u52a0\u8f09\u6578\u64da\u3002<\/p>\n<h3 id=\"1-%e8%a8%ad%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab-rBOjjLFhtl\">1. \u8a2d\u7f6e\u6578\u64da\u5eab<\/h3>\n<p>\u9996\u5148\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba\u201cusers\u201d\u7684\u6578\u64da\u8868\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n<ul>\n<li>id<\/li>\n<li>name<\/li>\n<li>email<\/li>\n<\/ul>\n<h3 id=\"2-%e5%89%b5%e5%bb%ba%e5%be%8c%e7%ab%afapi-rBOjjLFhtl\">2. \u5275\u5efa\u5f8c\u7aefAPI<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u5f8c\u7aefAPI\u4f86\u8655\u7406\u8acb\u6c42\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\nheader('Content-Type: application\/json');\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database\";\n\n\/\/ \u5275\u5efa\u9023\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"Connection failed: \" . $conn-&gt;connect_error);\n}\n\n$sql = \"SELECT id, name, email FROM users\";\n$result = $conn-&gt;query($sql);\n\n$users = array();\nif ($result-&gt;num_rows &gt; 0) {\n    while($row = $result-&gt;fetch_assoc()) {\n        $users[] = $row;\n    }\n}\necho json_encode($users);\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<h3 id=\"3-%e7%b7%a8%e5%af%ab%e5%89%8d%e7%ab%af%e4%bb%a3%e7%a2%bc-rBOjjLFhtl\">3. \u7de8\u5beb\u524d\u7aef\u4ee3\u78bc<\/h3>\n<p>\u7136\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5728\u524d\u7aef\u4f7f\u7528JavaScript\u4f86\u767c\u9001\u8acb\u6c42\u4e26\u8655\u7406\u97ff\u61c9\uff1a<\/p>\n<pre><code>&lt;script&gt;\nfunction loadUsers() {\n    var xhr = new XMLHttpRequest();\n    xhr.open(\"GET\", \"api\/get_users.php\", true);\n    xhr.onreadystatechange = function () {\n        if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) {\n            var users = JSON.parse(xhr.responseText);\n            var output = \"\";\n            for (var i = 0; i &lt; users.length; i++) {\n                output += \"&lt;p&gt;\" + users[i].name + \" (\" + users[i].email + \")&lt;\/p&gt;\";\n            }\n            document.getElementById(\"userList\").innerHTML = output;\n        }\n    };\n    xhr.send();\n}\n&lt;\/script&gt;\n&lt;button onclick=\"loadUsers()\"&gt;\u52a0\u8f09\u7528\u6236&lt;\/button&gt;\n&lt;div id=&quot;userList&quot;&gt;&lt;\/div&gt;<\/code><\/pre>\n<h2 id=\"%e5%af%a6%e9%9a%9b%e6%87%89%e7%94%a8%e4%b8%ad%e7%9a%84%e5%84%aa%e5%8b%a2-rBOjjLFhtl\">\u5be6\u969b\u61c9\u7528\u4e2d\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528Ajax\u52d5\u614b\u52a0\u8f09\u6578\u64da\u5eab\u6578\u64da\u7684\u512a\u52e2\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>\u63d0\u9ad8\u7528\u6236\u9ad4\u9a57\uff1a<\/strong>\u7528\u6236\u7121\u9700\u7b49\u5f85\u6574\u500b\u9801\u9762\u91cd\u65b0\u52a0\u8f09\uff0c\u64cd\u4f5c\u66f4\u52a0\u6d41\u66a2\u3002<\/li>\n<li><strong>\u6e1b\u5c11\u4f3a\u670d\u5668\u8ca0\u64d4\uff1a<\/strong>\u53ea\u52a0\u8f09\u5fc5\u8981\u7684\u6578\u64da\uff0c\u6e1b\u5c11\u4e86\u5e36\u5bec\u7684\u4f7f\u7528\u3002<\/li>\n<li><strong>\u589e\u5f37\u4e92\u52d5\u6027\uff1a<\/strong>\u53ef\u4ee5\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u7528\u6236\u4ea4\u4e92\uff0c\u5982\u5373\u6642\u641c\u7d22\u548c\u904e\u6ffe\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-rBOjjLFhtl\">\u7e3d\u7d50<\/h2>\n<p>Ajax\u6280\u8853\u70ba\u73fe\u4ee3\u7db2\u9801\u958b\u767c\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\uff0c\u4f7f\u5f97\u7121\u5237\u65b0\u66f4\u65b0\u6210\u70ba\u53ef\u80fd\u3002\u901a\u904e\u52d5\u614b\u52a0\u8f09\u6578\u64da\u5eab\u6578\u64da\uff0c\u958b\u767c\u8005\u80fd\u5920\u5275\u5efa\u66f4\u5177\u4e92\u52d5\u6027\u548c\u97ff\u61c9\u6027\u7684\u61c9\u7528\u3002\u7121\u8ad6\u662f\u7c21\u55ae\u7684\u7528\u6236\u5217\u8868\u9084\u662f\u8907\u96dc\u7684\u6578\u64da\u5206\u6790\u5de5\u5177\uff0cAjax\u90fd\u80fd\u5920\u6709\u6548\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u4f3a\u670d\u5668\u652f\u6301\u4ee5\u5be6\u73fe\u9019\u4e9b\u529f\u80fd\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u89e3\u6c7a\u65b9\u6848\uff0c\u70ba\u60a8\u7684\u61c9\u7528\u63d0\u4f9b\u5f37\u5927\u7684\u5f8c\u7aef\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4f7f\u7528Ajax\u5be6\u73fe\u7db2\u9801\u7121\u5237\u65b0\u66f4\u65b0\uff0c\u52d5\u614b\u52a0\u8f09\u6578\u64da\u5eab\u6578\u64da\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u8207\u7db2\u7ad9\u6027\u80fd\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-183404","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=183404"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183404\/revisions"}],"predecessor-version":[{"id":183405,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183404\/revisions\/183405"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}