{"id":22950,"date":"2024-11-21T11:31:14","date_gmt":"2024-11-21T03:31:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22950\/"},"modified":"2024-11-21T11:31:14","modified_gmt":"2024-11-21T03:31:14","slug":"thinkphp%e6%80%8e%e4%b9%88%e5%86%99%e5%a4%9a%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22950\/","title":{"rendered":"thinkphp\u600e\u4e48\u5199\u591a\u8bf7\u6c42"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u6211\u4eec\u5728\u5f00\u53d1 web \u5e94\u7528\u65f6\u9700\u8981\u8d8a\u6765\u8d8a\u591a\u5730\u5b9e\u73b0\u591a\u8bf7\u6c42\u529f\u80fd\u3002\u5728\u524d\u540e\u7aef\u5206\u79bb\u7684\u5f00\u53d1\u6a21\u5f0f\u4e0b\uff0c\u524d\u7aef\u9875\u9762\u9700\u8981\u5411\u540e\u7aef\u53d1\u9001\u591a\u6b21\u8bf7\u6c42\u6765\u83b7\u53d6\u4e0d\u540c\u7684\u6570\u636e\u6216\u5b9e\u73b0\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5728 php \u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u4f18\u79c0\u7684\u6846\u67b6\u2014\u2014thinkphp \u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002<\/p>\n<p>\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 ThinkPHP \u6846\u67b6\u6765\u5b9e\u73b0\u591a\u8bf7\u6c42\u529f\u80fd\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662f\u591a\u8bf7\u6c42\uff1f<\/p>\n<p>\u591a\u8bf7\u6c42\u662f\u6307\u5728\u4e00\u4e2a web \u5e94\u7528\u4e2d\uff0c\u4e00\u4e2a\u9875\u9762\u6216\u4e00\u4e2a\u529f\u80fd\u9700\u8981\u5411\u540e\u7aef\u53d1\u9001\u591a\u6b21\u8bf7\u6c42\uff0c\u4ee5\u83b7\u53d6\u4e0d\u540c\u7684\u6570\u636e\u6216\u5b9e\u73b0\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u591a\u8bf7\u6c42\u7684\u5b9e\u73b0\u901a\u5e38\u9700\u8981\u4f9d\u8d56 JavaScript \u6280\u672f\u548c AJAX \u6280\u672f\u3002<\/p>\n<p>\u4e8c\u3001ThinkPHP \u5b9e\u73b0\u591a\u8bf7\u6c42\u539f\u7406<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 AJAX \u6280\u672f\u6765\u5b9e\u73b0\u591a\u8bf7\u6c42\u529f\u80fd\u3002AJAX\uff08Asynchronous JavaScript and XML\uff09\u662f\u4e00\u79cd\u4f7f\u7528 JavaScript \u6280\u672f\u5411\u670d\u52a1\u5668\u53d1\u9001\u5f02\u6b65\u8bf7\u6c42\u548c\u63a5\u6536\u54cd\u5e94\u7684\u6280\u672f\u3002\u4f7f\u7528 AJAX \u6280\u672f\u53d1\u9001\u8bf7\u6c42\u65f6\uff0cWeb \u9875\u9762\u65e0\u9700\u5237\u65b0\uff0c\u4e5f\u65e0\u9700\u91cd\u8f7d\u6574\u4e2a\u9875\u9762\uff0c\u800c\u53ea\u9700\u8981\u66f4\u65b0\u9700\u8981\u66f4\u65b0\u7684\u90e8\u5206\u3002<\/p>\n<p>ThinkPHP \u6846\u67b6\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684 AJAX \u8bf7\u6c42\u65b9\u5f0f\uff0c\u5373\u901a\u8fc7 AjaxController \u7c7b\u7684 index \u65b9\u6cd5\u6765\u5904\u7406\u5f02\u6b65\u8bf7\u6c42\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a JSON \u6570\u636e\u683c\u5f0f\u7684\u54cd\u5e94\u3002\u6211\u4eec\u53ef\u4ee5\u5728 index \u65b9\u6cd5\u4e2d\u8fdb\u884c\u591a\u8bf7\u6c42\u7684\u5904\u7406\uff0c\u5e76\u8fd4\u56de\u591a\u4e2a JSON \u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<p>\u4e09\u3001ThinkPHP \u591a\u8bf7\u6c42\u7684\u5b9e\u73b0\u6b65\u9aa4<\/p>\n<ol>\n<li>\u5728 ThinkPHP \u6846\u67b6\u4e2d\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a AjaxController \u7684\u63a7\u5236\u5668\uff1a<\/li>\n<\/ol>\n<pre>php think make:controller AjaxController<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u521b\u5efa\u63a7\u5236\u5668\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728\u8be5\u63a7\u5236\u5668\u7c7b\u4e2d\u6dfb\u52a0\u4e00\u4e2a index \u65b9\u6cd5\uff0c\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u8bf7\u6c42\u3002\u5728\u8be5\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 thinkDb \u7c7b\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u83b7\u53d6\u6240\u9700\u7684\u6570\u636e\u3002\u6700\u540e\u5c06\u83b7\u53d6\u5230\u7684\u6570\u636e\u4ee5 JSON \u683c\u5f0f\u7684\u5f62\u5f0f\u8fd4\u56de\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u83b7\u53d6\u4e00\u4e2a\u5b66\u751f\u5217\u8868\uff0c\u4f7f\u7528 index \u65b9\u6cd5\u8fd4\u56de JSON \u683c\u5f0f\u7684\u6570\u636e\uff1a<\/p>\n<pre>\/\/ applicationindexcontrollerAjaxController.php\n\nnamespace appindexcontroller;\n\nuse thinkController;\n\nuse thinkDb;\n\nclass AjaxController extends Controller\n{\n    public function index()\n    {\n        \/\/ \u83b7\u53d6\u5b66\u751f\u5217\u8868\n        $students = Db::table('student')-&amp;gt;select();\n\n        \/\/ \u8fd4\u56de JSON \u683c\u5f0f\u7684\u6570\u636e\n        return json($students);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u5728\u524d\u7aef\u9875\u9762\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 JavaScript \u6280\u672f\u6765\u53d1\u9001\u5f02\u6b65\u8bf7\u6c42\uff0c\u4ece\u800c\u83b7\u53d6\u540e\u7aef\u8fd4\u56de\u7684 JSON \u6570\u636e\u3002\u5728 JavaScript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 XMLHttpRequest \u5bf9\u8c61\u6765\u53d1\u9001\u5f02\u6b65\u8bf7\u6c42\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u5411 AjaxController \u53d1\u9001\u8bf7\u6c42\uff0c\u5e76\u5728\u83b7\u53d6\u6570\u636e\u540e\u5c06\u6570\u636e\u663e\u793a\u5728\u9875\u9762\u4e2d\uff1a<\/p>\n<pre>\/\/ index.html\n\n&lt;script&gt;\n    var xhr = new XMLHttpRequest();\n    xhr.open('GET', '\/index\/ajax\/index', true); \/\/ \u53d1\u9001\u5f02\u6b65\u8bf7\u6c42\n    xhr.onreadystatechange = function() {\n        if(xhr.readyState === 4 &amp;&amp; xhr.status === 200) {\n            var data = JSON.parse(xhr.responseText); \/\/ \u83b7\u53d6\u540e\u53f0\u8fd4\u56de\u7684 JSON \u6570\u636e\n            \/\/ \u5c06\u6570\u636e\u663e\u793a\u5728\u9875\u9762\u4e0a\n            for(var i = 0; i &lt; data.length; i++) {\n                var tr = document.createElement('tr');\n                var td1 = document.createElement('td');\n                var td2 = document.createElement('td');\n                td1.innerHTML = data[i].name;\n                td2.innerHTML = data[i].age;\n                tr.appendChild(td1);\n                tr.appendChild(td2);\n                document.getElementById('studentList').appendChild(tr);\n            }\n        }\n    }\n    xhr.send();\n&lt;\/script&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<table>\n<thead>\n<tr>\n<th>\u59d3\u540d<\/th>\n<th>\u5e74\u9f84<\/th>\n<\/tr>\n<\/thead>\n<tbody><\/tbody>\n<\/table>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5411 AjaxController \u53d1\u9001 GET \u8bf7\u6c42\uff0c\u5e76\u76d1\u542c readyState \u548c status \u4e8b\u4ef6\u3002\u5f53 readyState \u5c5e\u6027\u503c\u53d8\u4e3a 4 \u65f6\uff0c\u8868\u793a\u8bf7\u6c42\u5df2\u7ecf\u5b8c\u6210\uff0cstatus \u5c5e\u6027\u503c\u4e3a 200 \u8868\u793a\u8bf7\u6c42\u6210\u529f\u3002\u6b64\u65f6\u53ef\u4ee5\u901a\u8fc7 responseText \u5c5e\u6027\u83b7\u53d6\u540e\u53f0\u8fd4\u56de\u7684 JSON \u6570\u636e\uff0c\u7136\u540e\u5c06\u6570\u636e\u52a8\u6001\u5730\u663e\u793a\u5728\u9875\u9762\u4e0a\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u4e09\u4e2a\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728 ThinkPHP \u6846\u67b6\u4e2d\u5b9e\u73b0\u591a\u8bf7\u6c42\u7684\u529f\u80fd\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u5728 index \u65b9\u6cd5\u4e2d\u8fdb\u884c\u591a\u8bf7\u6c42\u7684\u5904\u7406\uff0c\u5e76\u5c06\u591a\u4e2a JSON \u683c\u5f0f\u7684\u6570\u636e\u8fd4\u56de\u524d\u7aef\u9875\u9762\u3002<\/p>\n<p>\u56db\u3001\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 ThinkPHP \u6846\u67b6\u4e2d\u5b9e\u73b0\u591a\u8bf7\u6c42\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528 AJAX \u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u9875\u9762\u65e0\u9700\u91cd\u65b0\u52a0\u8f7d\u7684\u60c5\u51b5\u4e0b\uff0c\u5b9e\u73b0\u591a\u6b21\u5411\u540e\u7aef\u53d1\u9001\u8bf7\u6c42\u5e76\u83b7\u53d6\u54cd\u5e94\u7684\u529f\u80fd\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u8fdb\u4e00\u6b65\u4f18\u5316\u591a\u8bf7\u6c42\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5982\u4f7f\u7528\u9ad8\u6548\u7684\u6570\u636e\u5e93\u67e5\u8be2\u65b9\u5f0f\u3001\u5408\u7406\u5730\u5206\u7ec4\u53d1\u9001\u8bf7\u6c42\u7b49\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u591a\u8bf7\u6c42\u662f\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\u3002\u638c\u63e1\u4e86 ThinkPHP \u6846\u67b6\u4e2d\u7684\u591a\u8bf7\u6c42\u5b9e\u73b0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u52a0\u9ad8\u6548\u5730\u5b8c\u6210 web \u5e94\u7528\u7684\u5f00\u53d1\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u600e\u4e48\u5199\u591a\u8bf7\u6c42\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u4e0d\u65ad\u53d1\u5c55\uff0c\u6211\u4eec\u5728\u5f00\u53d1 web \u5e94\u7528\u65f6\u9700\u8981\u8d8a\u6765\u8d8a\u591a\u5730\u5b9e\u73b0\u591a\u8bf7\u6c42\u529f\u80fd\u3002\u5728\u524d\u540e\u7aef\u5206\u79bb\u7684\u5f00\u53d1\u6a21\u5f0f\u4e0b\uff0c\u524d\u7aef\u9875\u9762\u9700\u8981\u5411\u540e\u7aef\u53d1\u9001\u591a\u6b21\u8bf7\u6c42\u6765\u83b7\u53d6\u4e0d\u540c\u7684\u6570\u636e\u6216\u5b9e\u73b0\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5728 php \u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u4f18\u79c0\u7684\u6846\u67b6\u2014\u2014thinkphp \u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002 \u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 ThinkPHP \u6846\u67b6\u6765\u5b9e\u73b0\u591a\u8bf7\u6c42\u529f\u80fd\u3002 \u4e00\u3001\u4ec0\u4e48\u662f\u591a\u8bf7\u6c42\uff1f \u591a\u8bf7\u6c42\u662f\u6307\u5728\u4e00\u4e2a web \u5e94\u7528\u4e2d\uff0c\u4e00\u4e2a\u9875\u9762\u6216\u4e00\u4e2a\u529f\u80fd\u9700\u8981\u5411\u540e\u7aef\u53d1\u9001\u591a\u6b21\u8bf7\u6c42\uff0c\u4ee5\u83b7\u53d6\u4e0d\u540c\u7684\u6570\u636e\u6216\u5b9e\u73b0\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u591a\u8bf7\u6c42\u7684\u5b9e\u73b0\u901a\u5e38\u9700\u8981\u4f9d\u8d56 JavaScript \u6280\u672f\u548c AJAX \u6280\u672f\u3002 \u4e8c\u3001ThinkPHP \u5b9e\u73b0\u591a\u8bf7\u6c42\u539f\u7406 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 AJAX \u6280\u672f\u6765\u5b9e\u73b0\u591a\u8bf7\u6c42\u529f\u80fd\u3002AJAX\uff08Asynchronous JavaScript and XML\uff09\u662f\u4e00\u79cd\u4f7f\u7528 JavaScript \u6280\u672f\u5411\u670d\u52a1\u5668\u53d1\u9001\u5f02\u6b65\u8bf7\u6c42\u548c\u63a5\u6536\u54cd\u5e94\u7684\u6280\u672f\u3002\u4f7f\u7528 AJAX \u6280\u672f\u53d1\u9001\u8bf7\u6c42\u65f6\uff0cWeb \u9875\u9762\u65e0\u9700\u5237\u65b0\uff0c\u4e5f\u65e0\u9700\u91cd\u8f7d\u6574\u4e2a\u9875\u9762\uff0c\u800c\u53ea\u9700\u8981\u66f4\u65b0\u9700\u8981\u66f4\u65b0\u7684\u90e8\u5206\u3002 ThinkPHP \u6846\u67b6\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684 AJAX \u8bf7\u6c42\u65b9\u5f0f\uff0c\u5373\u901a\u8fc7 AjaxController \u7c7b\u7684 index \u65b9\u6cd5\u6765\u5904\u7406\u5f02\u6b65\u8bf7\u6c42\uff0c\u8be5\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a JSON \u6570\u636e\u683c\u5f0f\u7684\u54cd\u5e94\u3002\u6211\u4eec\u53ef\u4ee5\u5728 index \u65b9\u6cd5\u4e2d\u8fdb\u884c\u591a\u8bf7\u6c42\u7684\u5904\u7406\uff0c\u5e76\u8fd4\u56de\u591a\u4e2a JSON \u683c\u5f0f\u7684\u6570\u636e\u3002 \u4e09\u3001ThinkPHP \u591a\u8bf7\u6c42\u7684\u5b9e\u73b0\u6b65\u9aa4 \u5728 ThinkPHP \u6846\u67b6\u4e2d\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a AjaxController \u7684\u63a7\u5236\u5668\uff1a php [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22950","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22950"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22950\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}