{"id":171295,"date":"2024-11-07T01:33:19","date_gmt":"2024-11-06T17:33:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/171295\/"},"modified":"2024-11-07T01:33:20","modified_gmt":"2024-11-06T17:33:20","slug":"%e5%89%8d%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e4%b9%8brequest%e7%8d%b2%e5%8f%96-request%e7%8d%b2%e5%8f%96%e5%89%8d%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/171295\/","title":{"rendered":"\u524d\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u4e4brequest\u7372\u53d6 (request\u7372\u53d6\u524d\u53f0\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%89%8d%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e4%b9%8brequest%e7%8d%b2%e5%8f%96-request%e7%8d%b2%e5%8f%96%e5%89%8d%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab-nSqXnDMOee\">\u524d\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u4e4brequest\u7372\u53d6 (request\u7372\u53d6\u524d\u53f0\u6578\u64da\u5eab)<\/h1>\n<p>\u5728\u73fe\u4ee3\u7db2\u9801\u958b\u767c\u4e2d\uff0c\u524d\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u7121\u8ad6\u662f\u7528\u65bc\u986f\u793a\u7528\u6236\u8cc7\u6599\u3001\u7522\u54c1\u4fe1\u606f\uff0c\u9084\u662f\u8655\u7406\u7528\u6236\u8acb\u6c42\uff0c\u524d\u53f0\u6578\u64da\u5eab\u7684\u6709\u6548\u64cd\u4f5c\u90fd\u80fd\u986f\u8457\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u901a\u904erequest\u7372\u53d6\u524d\u53f0\u6578\u64da\u5eab\u7684\u6578\u64da\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afrequest%e7%8d%b2%e5%8f%96%ef%bc%9f-nSqXnDMOee\">\u4ec0\u9ebc\u662frequest\u7372\u53d6\uff1f<\/h2>\n<p>\u5728\u7db2\u9801\u958b\u767c\u4e2d\uff0crequest\u901a\u5e38\u6307\u7684\u662f\u7528\u6236\u7aef\u5411\u4f3a\u670d\u5668\u767c\u9001\u7684\u8acb\u6c42\u3002\u9019\u4e9b\u8acb\u6c42\u53ef\u4ee5\u662fGET\u8acb\u6c42\u6216POST\u8acb\u6c42\uff0c\u6839\u64da\u4e0d\u540c\u7684\u9700\u6c42\u4f86\u7372\u53d6\u6216\u63d0\u4ea4\u6578\u64da\u3002\u524d\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6838\u5fc3\u5728\u65bc\u5982\u4f55\u6709\u6548\u5730\u8655\u7406\u9019\u4e9b\u8acb\u6c42\uff0c\u4ee5\u4fbf\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n<h2 id=\"%e5%89%8d%e5%8f%b0%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%b5%81%e7%a8%8b-nSqXnDMOee\">\u524d\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u57fa\u672c\u6d41\u7a0b<\/h2>\n<ul>\n<li><strong>\u7528\u6236\u767c\u9001\u8acb\u6c42\uff1a<\/strong>\u7528\u6236\u5728\u524d\u7aef\u754c\u9762\u4e0a\u9032\u884c\u64cd\u4f5c\uff0c\u4f8b\u5982\u9ede\u64ca\u6309\u9215\u6216\u63d0\u4ea4\u8868\u55ae\uff0c\u9019\u6703\u89f8\u767c\u4e00\u500bHTTP\u8acb\u6c42\u3002<\/li>\n<li><strong>\u4f3a\u670d\u5668\u63a5\u6536\u8acb\u6c42\uff1a<\/strong>\u4f3a\u670d\u5668\u63a5\u6536\u5230\u8acb\u6c42\u5f8c\uff0c\u6839\u64da\u8acb\u6c42\u7684\u985e\u578b\u548c\u5167\u5bb9\u9032\u884c\u76f8\u61c9\u7684\u8655\u7406\u3002<\/li>\n<li><strong>\u67e5\u8a62\u6578\u64da\u5eab\uff1a<\/strong>\u4f3a\u670d\u5668\u6839\u64da\u8acb\u6c42\u7684\u53c3\u6578\u67e5\u8a62\u6578\u64da\u5eab\uff0c\u7372\u53d6\u6240\u9700\u7684\u6578\u64da\u3002<\/li>\n<li><strong>\u8fd4\u56de\u97ff\u61c9\uff1a<\/strong>\u4f3a\u670d\u5668\u5c07\u67e5\u8a62\u7d50\u679c\u8fd4\u56de\u7d66\u7528\u6236\u7aef\uff0c\u901a\u5e38\u4ee5JSON\u6216HTML\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8ajax%e9%80%b2%e8%a1%8crequest%e7%8d%b2%e5%8f%96-nSqXnDMOee\">\u4f7f\u7528AJAX\u9032\u884crequest\u7372\u53d6<\/h2>\n<p>AJAX\uff08Asynchronous JavaScript and XML\uff09\u662f\u4e00\u7a2e\u5728\u4e0d\u91cd\u65b0\u52a0\u8f09\u6574\u500b\u9801\u9762\u7684\u60c5\u6cc1\u4e0b\uff0c\u8207\u4f3a\u670d\u5668\u9032\u884c\u7570\u6b65\u4ea4\u4e92\u7684\u6280\u8853\u3002\u9019\u4f7f\u5f97\u524d\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u66f4\u52a0\u6d41\u66a2\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528AJAX\u9032\u884crequest\u7372\u53d6\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nfunction fetchData() {\n    var xhr = new XMLHttpRequest();\n    xhr.open(\"GET\", \"https:\/\/example.com\/api\/data\", true);\n    xhr.onreadystatechange = function() {\n        if (xhr.readyState == 4 &amp;&amp; xhr.status == 200) {\n            var data = JSON.parse(xhr.responseText);\n            console.log(data);\n            \/\/ \u5728\u9019\u88e1\u8655\u7406\u7372\u53d6\u5230\u7684\u6578\u64da\n        }\n    };\n    xhr.send();\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500bXMLHttpRequest\u5c0d\u8c61\uff0c\u4e26\u4f7f\u7528GET\u65b9\u6cd5\u5411\u4f3a\u670d\u5668\u8acb\u6c42\u6578\u64da\u3002\u7576\u8acb\u6c42\u5b8c\u6210\u5f8c\uff0c\u6211\u5011\u89e3\u6790\u8fd4\u56de\u7684JSON\u6578\u64da\u4e26\u9032\u884c\u76f8\u61c9\u7684\u8655\u7406\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8fetch-api%e9%80%b2%e8%a1%8crequest%e7%8d%b2%e5%8f%96-nSqXnDMOee\">\u4f7f\u7528Fetch API\u9032\u884crequest\u7372\u53d6<\/h2>\n<p>Fetch API\u662f\u73fe\u4ee3\u700f\u89bd\u5668\u4e2d\u63d0\u4f9b\u7684\u4e00\u7a2e\u66f4\u7c21\u6f54\u7684\u65b9\u5f0f\u4f86\u9032\u884cHTTP\u8acb\u6c42\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Fetch API\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nfetch(\"https:\/\/example.com\/api\/data\")\n    .then(response =&gt; {\n        if (!response.ok) {\n            throw new Error(\"Network response was not ok\");\n        }\n        return response.json();\n    })\n    .then(data =&gt; {\n        console.log(data);\n        \/\/ \u5728\u9019\u88e1\u8655\u7406\u7372\u53d6\u5230\u7684\u6578\u64da\n    })\n    .catch(error =&gt; {\n        console.error(\"There was a problem with the fetch operation:\", error);\n    });\n<\/code><\/pre>\n<p>Fetch API\u7684\u4f7f\u7528\u4f7f\u5f97\u4ee3\u78bc\u66f4\u52a0\u7c21\u6f54\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u932f\u8aa4\u8655\u7406\u6a5f\u5236\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%e7%9a%84%e5%ae%89%e5%85%a8%e6%80%a7-nSqXnDMOee\">\u6578\u64da\u5eab\u67e5\u8a62\u7684\u5b89\u5168\u6027<\/h2>\n<p>\u5728\u9032\u884c\u524d\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u8003\u91cf\u3002\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\u53ef\u4ee5\u6709\u6548\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528PHP\u548cMySQLi\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n$mysqli = new mysqli(\"localhost\", \"user\", \"password\", \"database\");\n$stmt = $mysqli-&gt;prepare(\"SELECT * FROM users WHERE id = ?\");\n$stmt-&gt;bind_param(\"i\", $userId);\n$stmt-&gt;execute();\n$result = $stmt-&gt;get_result();\nwhile ($row = $result-&gt;fetch_assoc()) {\n    echo $row['name'];\n}\n$stmt-&gt;close();\n$mysqli-&gt;close();\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u5b89\u5168\u5730\u67e5\u8a62\u6578\u64da\u5eab\uff0c\u9019\u6a23\u53ef\u4ee5\u6709\u6548\u9632\u6b62SQL\u6ce8\u5165\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-nSqXnDMOee\">\u7e3d\u7d50<\/h2>\n<p>\u524d\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u4e4brequest\u7372\u53d6\u662f\u73fe\u4ee3\u7db2\u9801\u958b\u767c\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u901a\u904e\u4f7f\u7528AJAX\u6216Fetch API\uff0c\u6211\u5011\u53ef\u4ee5\u5be6\u73fe\u6d41\u66a2\u7684\u7528\u6236\u9ad4\u9a57\uff0c\u540c\u6642\u78ba\u4fdd\u6578\u64da\u7684\u5b89\u5168\u6027\u3002\u4e86\u89e3\u5982\u4f55\u6709\u6548\u5730\u8655\u7406\u7528\u6236\u8acb\u6c42\u4e26\u5f9e\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u6578\u64da\uff0c\u5c0d\u65bc\u958b\u767c\u9ad8\u6548\u7684\u7db2\u9801\u61c9\u7528\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bcVPS\u3001<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u3001<a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\u6216\u5176\u4ed6\u76f8\u95dc\u670d\u52d9\u611f\u8208\u8da3\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u5728\u524d\u53f0\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u6709\u6548\u7372\u53d6\u8acb\u6c42(request)\uff0c\u63d0\u5347\u6578\u64da\u8655\u7406\u6548\u7387\u8207\u6e96\u78ba\u6027\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-171295","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171295","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=171295"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171295\/revisions"}],"predecessor-version":[{"id":171296,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/171295\/revisions\/171296"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=171295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=171295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=171295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}