{"id":1256,"date":"2026-01-06T16:05:38","date_gmt":"2026-01-06T08:05:38","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1256"},"modified":"2026-01-06T16:05:38","modified_gmt":"2026-01-06T08:05:38","slug":"thinkphp%e4%b8%ad%e4%bd%bf%e7%94%a8ajax%e6%8e%a5%e6%94%b6json%e6%95%b0%e6%8d%ae%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/thinkphp%e4%b8%ad%e4%bd%bf%e7%94%a8ajax%e6%8e%a5%e6%94%b6json%e6%95%b0%e6%8d%ae%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"ThinkPHP\u4e2d\u4f7f\u7528ajax\u63a5\u6536json\u6570\u636e\u7684\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u5728 ThinkPHP \u4e2d\u7528 Ajax \u63a5\u6536 JSON \u6570\u636e\u7684\u5b8c\u6574\u3001\u5b9e\u6218\u7ea7\u8bb2\u89e3<\/strong>\uff0c\u8986\u76d6 <strong>TP5 \/ TP6<\/strong> \u5e38\u89c1\u5199\u6cd5\ud83d\udc47<br>\u6211\u4f1a\u6309 <strong>\u524d\u7aef \u2192 \u540e\u7aef \u2192 \u5e38\u89c1\u5751 \u2192 \u8c03\u8bd5\u6280\u5de7<\/strong> \u6765\u8bb2\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001Ajax \u53d1\u9001 JSON \u7684\u6b63\u786e\u65b9\u5f0f\uff08\u524d\u7aef\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u6807\u51c6\u5199\u6cd5\uff08jQuery \u793a\u4f8b\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$.ajax({\n    url: &#039;\/index\/user\/save&#039;,\n    type: &#039;POST&#039;,\n    data: JSON.stringify({\n        username: &#039;tom&#039;,\n        age: 18\n    }),\n    contentType: &#039;application\/json&#039;,\n    dataType: &#039;json&#039;,\n    success(res) {\n        console.log(res);\n    }\n});\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u26a0\ufe0f \u4e24\u4e2a\u5173\u952e\u70b9\uff08\u5fc5\u987b\uff09<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9879<\/th><th>\u5fc5\u987b<\/th><\/tr><\/thead><tbody><tr><td><code>JSON.stringify()<\/code><\/td><td>\u662f<\/td><\/tr><tr><td><code>contentType: application\/json<\/code><\/td><td>\u662f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u539f\u751f fetch \u5199\u6cd5\uff08\u63a8\u8350\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfetch(&#039;\/index\/user\/save&#039;, {\n    method: &#039;POST&#039;,\n    headers: {\n        &#039;Content-Type&#039;: &#039;application\/json&#039;\n    },\n    body: JSON.stringify({\n        username: &#039;tom&#039;,\n        age: 18\n    })\n})\n.then(res =&gt; res.json())\n.then(data =&gt; console.log(data));\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001ThinkPHP 6 \u63a5\u6536 JSON \u6570\u636e\uff08\u63a8\u8350\u65b9\u5f0f\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 Controller \u4e2d\u6b63\u786e\u5199\u6cd5<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuse think\\Request;\n\npublic function save(Request $request)\n{\n    $data = $request-&gt;getInput(); \/\/ \u8bfb\u53d6 JSON \u539f\u59cb\u6570\u636e\n    return json(&#x5B;\n        &#039;code&#039; =&gt; 0,\n        &#039;data&#039; =&gt; $data\n    ]);\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u7b49\u4ef7\u5199\u6cd5\uff08\u66f4\u5e38\u7528\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic function save()\n{\n    $data = request()-&gt;param(); \/\/ JSON \/ \u8868\u5355\u90fd\u80fd\u63a5\n    return json($data);\n}\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>TP6 \u4f1a\u81ea\u52a8\u89e3\u6790 application\/json<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001ThinkPHP 5 \u63a5\u6536 JSON \u6570\u636e\u7684\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 1\uff1a<code>getContent()<\/code>\uff08\u6700\u7a33\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic function save()\n{\n    $json = file_get_contents(&quot;php:\/\/input&quot;);\n    $data = json_decode($json, true);\n\n    return json($data);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u65b9\u6cd5 2\uff1aRequest \u5bf9\u8c61<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nuse think\\Request;\n\npublic function save(Request $request)\n{\n    $data = $request-&gt;param();\n    return json($data);\n}\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f TP5 \u5bf9 JSON \u652f\u6301\u4e0d\u5982 TP6 \u7a33\u5b9a\uff0c<strong>\u63a8\u8350\u65b9\u5f0f 1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u63a5\u6536 JSON + \u8868\u5355\u901a\u7528\u65b9\u6848\uff08\u901a\u6740\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic function save()\n{\n    $request = request();\n\n    if ($request-&gt;isJson()) {\n        $data = $request-&gt;getInput();\n    } else {\n        $data = $request-&gt;post();\n    }\n\n    return json($data);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u5b8c\u6574\u793a\u4f8b\uff08\u524d\u540e\u7aef\u914d\u5408\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u524d\u7aef<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfetch(&#039;\/index\/user\/login&#039;, {\n    method: &#039;POST&#039;,\n    headers: {\n        &#039;Content-Type&#039;: &#039;application\/json&#039;\n    },\n    body: JSON.stringify({\n        username: &#039;admin&#039;,\n        password: &#039;123456&#039;\n    })\n});\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u540e\u7aef\uff08TP6\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic function login()\n{\n    $data = request()-&gt;param();\n\n    if ($data&#x5B;&#039;username&#039;] !== &#039;admin&#039;) {\n        return json(&#x5B;&#039;code&#039; =&gt; 1, &#039;msg&#039; =&gt; &#039;\u7528\u6237\u4e0d\u5b58\u5728&#039;]);\n    }\n\n    return json(&#x5B;&#039;code&#039; =&gt; 0, &#039;msg&#039; =&gt; &#039;\u767b\u5f55\u6210\u529f&#039;]);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u6700\u5bb9\u6613\u8e29\u7684 5 \u4e2a\u5751\uff08\u91cd\u70b9\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c 1. \u7528 <code>$_POST<\/code> \u63a5 JSON<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$_POST&#x5B;&#039;username&#039;]; \/\/ \u274c \u6c38\u8fdc\u662f\u7a7a\n\n<\/pre><\/div>\n\n\n<p>\u2714 JSON <strong>\u4e0d\u662f form-data<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c 2. \u5fd8\u8bb0\u8bbe\u7f6e <code>contentType<\/code><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncontentType: &#039;application\/json&#039;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c 3. JSON \u672a stringify<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndata: {a:1} \/\/ \u274c\ndata: JSON.stringify({a:1}) \/\/ \u2705\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c 4. ThinkPHP \u62e6\u622a OPTIONS\uff08\u8de8\u57df\uff09<\/h3>\n\n\n\n<p>\u9700\u8981\u52a0\u8de8\u57df\u4e2d\u95f4\u4ef6\u6216\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nheader(&#039;Access-Control-Allow-Origin:*&#039;);\nheader(&#039;Access-Control-Allow-Headers: Content-Type&#039;);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u274c 5. \u8bf7\u6c42\u65b9\u5f0f\u4e0d\u5bf9<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$request-&gt;post(); \/\/ \u53ea\u80fd\u63a5 form\n$request-&gt;param(); \/\/ JSON + form \u90fd\u80fd\u63a5\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u8c03\u8bd5\u6280\u5de7\uff08\u975e\u5e38\u5b9e\u7528\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6253\u5370\u539f\u59cb JSON<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndump(file_get_contents(&quot;php:\/\/input&quot;));\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5224\u65ad\u662f\u5426 JSON<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nrequest()-&gt;isJson();\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u5feb\u901f\u5bf9\u7167\u8868<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u63a8\u8350<\/th><\/tr><\/thead><tbody><tr><td>TP6<\/td><td><code>request()-&gt;param()<\/code><\/td><\/tr><tr><td>TP5<\/td><td><code>php:\/\/input<\/code><\/td><\/tr><tr><td>Ajax JSON<\/td><td><code>application\/json<\/code><\/td><\/tr><tr><td>\u901a\u7528<\/td><td><code>isJson()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u4e00\u53e5\u8bdd\u603b\u7ed3<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>ThinkPHP \u63a5\u6536 Ajax JSON \u7684\u6838\u5fc3\u53ea\u6709\u4e00\u53e5\u8bdd\uff1a\u524d\u7aef\u7528 <code>application\/json<\/code>\uff0c\u540e\u7aef\u4e0d\u7528 <code>$_POST<\/code><\/strong><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u5728 ThinkPHP \u4e2d\u7528 Ajax \u63a5\u6536 JSON \u6570\u636e\u7684\u5b8c\u6574\u3001&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/01\/06\/thinkphp%e4%b8%ad%e4%bd%bf%e7%94%a8ajax%e6%8e%a5%e6%94%b6json%e6%95%b0%e6%8d%ae%e7%9a%84%e6%96%b9%e6%b3%95\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-1256","post","type-post","status-publish","format-standard","hentry","category-php"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1256"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1256\/revisions"}],"predecessor-version":[{"id":1257,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1256\/revisions\/1257"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}