{"id":55493,"date":"2024-10-10T03:39:16","date_gmt":"2024-10-09T19:39:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/55493\/"},"modified":"2024-10-10T03:39:16","modified_gmt":"2024-10-09T19:39:16","slug":"wordpress-%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8-get-%e9%80%b2%e8%a1%8c-ajax-%e8%ab%8b%e6%b1%82","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/55493\/","title":{"rendered":"WordPress \u4f7f\u7528\u6280\u5de7\uff1a\u4f7f\u7528 GET \u9032\u884c AJAX \u8acb\u6c42"},"content":{"rendered":"<h1 id=\"wordpress-%e4%bd%bf%e7%94%a8%e6%8a%80%e5%b7%a7%ef%bc%9a%e4%bd%bf%e7%94%a8-get-%e9%80%b2%e8%a1%8c-ajax-%e8%ab%8b%e6%b1%82-qTgZbVIKjO\">WordPress \u4f7f\u7528\u6280\u5de7\uff1a\u4f7f\u7528 GET \u9032\u884c AJAX \u8acb\u6c42<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0cAJAX\uff08Asynchronous JavaScript and XML\uff09\u6280\u8853\u5df2\u6210\u70ba\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u7684\u91cd\u8981\u5de5\u5177\u3002\u7279\u5225\u662f\u5728\u4f7f\u7528 WordPress \u6642\uff0c\u900f\u904e AJAX \u53ef\u4ee5\u5be6\u73fe\u7121\u9700\u91cd\u65b0\u52a0\u8f09\u9801\u9762\u7684\u52d5\u614b\u5167\u5bb9\u66f4\u65b0\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728 WordPress \u4e2d\u4f7f\u7528 GET \u65b9\u6cd5\u9032\u884c AJAX \u8acb\u6c42\uff0c\u4e26\u63d0\u4f9b\u5be6\u7528\u7684\u7bc4\u4f8b\u548c\u6280\u5de7\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-ajax%ef%bc%9f-qTgZbVIKjO\">\u4ec0\u9ebc\u662f AJAX\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\u6578\u64da\u4ea4\u63db\u3002\u9019\u610f\u5473\u8457\u7528\u6236\u53ef\u4ee5\u5728\u4e0d\u6253\u65b7\u7576\u524d\u64cd\u4f5c\u7684\u60c5\u6cc1\u4e0b\uff0c\u7372\u53d6\u65b0\u7684\u6578\u64da\u6216\u66f4\u65b0\u5167\u5bb9\u3002<\/p>\n<h2 id=\"%e5%9c%a8-wordpress-%e4%b8%ad%e4%bd%bf%e7%94%a8-ajax-%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-qTgZbVIKjO\">\u5728 WordPress \u4e2d\u4f7f\u7528 AJAX \u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<p>\u5728 WordPress \u4e2d\u5be6\u73fe AJAX \u8acb\u6c42\u7684\u904e\u7a0b\u4e3b\u8981\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li>\u8a3b\u518a AJAX \u884c\u70ba<\/li>\n<li>\u7de8\u5beb JavaScript \u4ee3\u78bc\u4ee5\u767c\u9001\u8acb\u6c42<\/li>\n<li>\u8655\u7406\u8acb\u6c42\u4e26\u8fd4\u56de\u97ff\u61c9<\/li>\n<\/ul>\n<h3 id=\"1-%e8%a8%bb%e5%86%8a-ajax-%e8%a1%8c%e7%82%ba-qTgZbVIKjO\">1. \u8a3b\u518a AJAX \u884c\u70ba<\/h3>\n<p>\u5728 WordPress \u4e2d\uff0c\u60a8\u9700\u8981\u4f7f\u7528 <code>add_action<\/code> \u51fd\u6578\u4f86\u8a3b\u518a AJAX \u884c\u70ba\u3002\u9019\u53ef\u4ee5\u5728\u4e3b\u984c\u7684 <code>functions.php<\/code> \u6587\u4ef6\u4e2d\u5b8c\u6210\u3002\u4ee5\u4e0b\u662f\u8a3b\u518a AJAX \u884c\u70ba\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\nadd_action('wp_ajax_my_action', 'my_action_callback');\nadd_action('wp_ajax_nopriv_my_action', 'my_action_callback');\n\nfunction my_action_callback() {\n    \/\/ \u8655\u7406\u8acb\u6c42\u7684\u908f\u8f2f\n    $response = array('message' =&gt; 'Hello, World!');\n    echo json_encode($response);\n    wp_die(); \/\/ \u7d50\u675f AJAX \u8acb\u6c42\n}\n<\/code><\/pre>\n<h3 id=\"2-%e7%b7%a8%e5%af%ab-javascript-%e4%bb%a3%e7%a2%bc%e4%bb%a5%e7%99%bc%e9%80%81%e8%ab%8b%e6%b1%82-qTgZbVIKjO\">2. \u7de8\u5beb JavaScript \u4ee3\u78bc\u4ee5\u767c\u9001\u8acb\u6c42<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u7de8\u5beb JavaScript \u4ee3\u78bc\u4f86\u767c\u9001 AJAX \u8acb\u6c42\u3002\u9019\u901a\u5e38\u5728\u4e3b\u984c\u7684 JavaScript \u6587\u4ef6\u4e2d\u9032\u884c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 jQuery \u767c\u9001 GET \u8acb\u6c42\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\njQuery(document).ready(function($) {\n    $('#my-button').on('click', function() {\n        $.get(ajaxurl, {\n            action: 'my_action'\n        }, function(response) {\n            \/\/ \u8655\u7406\u97ff\u61c9\n            var data = JSON.parse(response);\n            $('#my-div').html(data.message);\n        });\n    });\n});\n<\/code><\/pre>\n<h3 id=\"3-%e8%99%95%e7%90%86%e8%ab%8b%e6%b1%82%e4%b8%a6%e8%bf%94%e5%9b%9e%e9%9f%bf%e6%87%89-qTgZbVIKjO\">3. \u8655\u7406\u8acb\u6c42\u4e26\u8fd4\u56de\u97ff\u61c9<\/h3>\n<p>\u5728\u4e0a\u9762\u7684 PHP \u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5df2\u7d93\u8655\u7406\u4e86\u8acb\u6c42\u4e26\u8fd4\u56de\u4e86\u4e00\u500b JSON \u97ff\u61c9\u3002\u7576\u7528\u6236\u9ede\u64ca\u6309\u9215\u6642\uff0cJavaScript \u4ee3\u78bc\u6703\u767c\u9001\u8acb\u6c42\uff0c\u4e26\u5728\u6536\u5230\u97ff\u61c9\u5f8c\u66f4\u65b0\u9801\u9762\u5167\u5bb9\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-get-%e6%96%b9%e6%b3%95%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-qTgZbVIKjO\">\u4f7f\u7528 GET \u65b9\u6cd5\u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u4f7f\u7528 GET \u65b9\u6cd5\u9032\u884c AJAX \u8acb\u6c42\u6642\uff0c\u6709\u5e7e\u500b\u6ce8\u610f\u4e8b\u9805\uff1a<\/p>\n<ul>\n<li><strong>\u6578\u64da\u5927\u5c0f\u9650\u5236\uff1a<\/strong>GET \u8acb\u6c42\u7684 URL \u9577\u5ea6\u6709\u9650\u5236\uff0c\u56e0\u6b64\u4e0d\u9069\u5408\u50b3\u9001\u5927\u91cf\u6578\u64da\u3002<\/li>\n<li><strong>\u5b89\u5168\u6027\uff1a<\/strong>\u7531\u65bc GET \u8acb\u6c42\u7684\u6578\u64da\u6703\u986f\u793a\u5728 URL \u4e2d\uff0c\u56e0\u6b64\u4e0d\u61c9\u7528\u65bc\u50b3\u9001\u654f\u611f\u4fe1\u606f\u3002<\/li>\n<li><strong>\u7de9\u5b58\u554f\u984c\uff1a<\/strong>GET \u8acb\u6c42\u53ef\u80fd\u6703\u88ab\u700f\u89bd\u5668\u7de9\u5b58\uff0c\u9019\u53ef\u80fd\u6703\u5f71\u97ff\u6578\u64da\u7684\u5373\u6642\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-qTgZbVIKjO\">\u7e3d\u7d50<\/h2>\n<p>\u900f\u904e\u4f7f\u7528 GET \u65b9\u6cd5\u9032\u884c AJAX \u8acb\u6c42\uff0cWordPress \u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5be6\u73fe\u52d5\u614b\u5167\u5bb9\u66f4\u65b0\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u8a3b\u518a AJAX \u884c\u70ba\u3001\u7de8\u5beb JavaScript \u4ee3\u78bc\u4ee5\u53ca\u8655\u7406\u8acb\u6c42\u7684\u57fa\u672c\u6b65\u9a5f\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u4f86\u652f\u6301\u60a8\u7684 WordPress \u7db2\u7ad9\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u89e3\u6c7a\u65b9\u6848\uff0c\u52a9\u60a8\u8f15\u9b06\u7ba1\u7406\u7db2\u7ad9\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 WordPress \u4e2d\u4f7f\u7528 GET \u65b9\u6cd5\u9032\u884c AJAX \u8acb\u6c42\uff0c\u63d0\u5347\u7db2\u7ad9\u4e92\u52d5\u6027\u8207\u6027\u80fd\uff0c\u7c21\u5316\u6578\u64da\u8655\u7406\u6d41\u7a0b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-55493","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/55493","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=55493"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/55493\/revisions"}],"predecessor-version":[{"id":55494,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/55493\/revisions\/55494"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=55493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=55493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=55493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}