{"id":6155,"date":"2022-04-05T18:41:27","date_gmt":"2022-04-05T10:41:27","guid":{"rendered":"https:\/\/egonlin.com\/?p=6155"},"modified":"2022-04-16T19:48:36","modified_gmt":"2022-04-16T11:48:36","slug":"%e7%ac%ac%e5%8d%81%e4%b8%89%e8%8a%82%ef%bc%9aajax","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=6155","title":{"rendered":"\u7b2c\u5341\u4e09\u8282\uff1aAjax"},"content":{"rendered":"<h1>Ajax<\/h1>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/Ajax1.jpg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/Ajax1.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<h2>\u4e00\u3001Ajax\u4ecb\u7ecd<\/h2>\n<h3>1\u3001\u4ec0\u4e48\u662fajax<\/h3>\n<p>\u7b80\u5355\u5730\u8bb2\uff0cajax\u5c31\u662f\u4e00\u79cd\u524d\u7aef\u6280\u672f\uff0c\u7528\u4e8e\u671d\u7740\u540e\u7aef\u7a0b\u5e8f\u53d1\u9001\u8bf7\u6c42\u548c\u6570\u636e<\/p>\n<pre><code class=\"language-python\"># \u603b\u7ed3\u4e0b\u6765\uff0c\u524d\u7aef\u5411\u540e\u7aef\u53d1\u9001\u8bf7\u6c42\u7684\u65b9\u5f0f\u65e0\u975e\u4ee5\u4e0b\u51e0\u79cd\uff0cajax\u6280\u672f\u4fbf\u662f\u5176\u4e2d\u4e00\u79cd\n1\u3001\u6d4f\u89c8\u5668\u5730\u5740\u680f\u8f93\u5165url\u5730\u5740\n    \u8bf7\u6c42\u65b9\u5f0f\u9ed8\u8ba4\u5e76\u4e14\u53ea\u80fd\u662f\uff1aget\u8bf7\u6c42\n\n2\u3001a\u6807\u7b7e\n    \u8bf7\u6c42\u65b9\u5f0f\u9ed8\u8ba4\u5e76\u4e14\u53ea\u80fd\u662f\uff1aget\u8bf7\u6c42\n\n3\u3001form\u8868\u5355\n    \u8bf7\u6c42\u65b9\u5f0f\u53ef\u4ee5\u662f\uff1aget\u4e0epost\u8bf7\u6c42\n\n4\u3001ajax\u6280\u672f\n    \u8bf7\u6c42\u65b9\u5f0f\u53ef\u4ee5\u662f\uff1aget\u4e0epost\u8bf7\u6c42\n\n    AJAX\uff08Asynchronous Javascript And XML\uff09\u7ffb\u8bd1\u6210\u4e2d\u6587\u5c31\u662f\u201c\u5f02\u6b65Javascript\u548cXML\u201d\u3002\n    \u5373\u4f7f\u7528Javascript\u8bed\u8a00\u4e0e\u670d\u52a1\u5668\u8fdb\u884c\u5f02\u6b65\u4ea4\u4e92\uff0c\u4f20\u8f93\u7684\u6570\u636e\u4e3aXML\n    \u5f53\u7136\uff0c\u4f20\u8f93\u7684\u6570\u636e\u4e0d\u53ea\u662fXML,\u73b0\u5728\u66f4\u591a\u4f7f\u7528json\u6570\u636e\u3002<\/code><\/pre>\n<p>\u5176\u5b9eAjax\u4e0d\u662f\u5177\u4f53\u7684\u67d0\u4e00\u4e2a\u6280\u672f\uff0c\u800c\u662f\u4e0b\u8ff0\u51e0\u79cd\u6280\u672f\u7684\u878d\u5408\uff0c\u6bcf\u79cd\u6280\u672f\u90fd\u6709\u5176\u72ec\u7279\u4e4b\u5904\uff0c\u5408\u5728\u4e00\u8d77\u5c31\u6210\u4e86\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u65b0\u6280\u672f<\/p>\n<p>\u2022 (1)XHTML\u548cCSS\u7684\u57fa\u4e8e\u6807\u51c6\u7684\u8868\u793a\u6280\u672f<\/p>\n<p>\u2022 (2)DOM\u8fdb\u884c\u52a8\u6001\u663e\u793a\u548c\u4ea4\u4e92<\/p>\n<p>\u2022 (3)XML\u548cXSLT\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u548c\u5904\u7406<\/p>\n<p>\u2022 (4)XMLHttpRequest\u8fdb\u884c\u5f02\u6b65\u6570\u636e\u68c0\u7d22<\/p>\n<p>\u2022 (5)Javascript\u5c06\u4ee5\u4e0a\u6280\u672f\u878d\u5408\u5728\u4e00\u8d77<\/p>\n<p>\u6240\u4ee5ajax\u4f9d\u8d56\u4f9d\u8d56\u6d4f\u89c8\u5668\u7684 JavaScript \u548cXML<\/p>\n<h3>2\u3001\u4e3a\u4f55\u8981\u7528ajax<\/h3>\n<p>\u5728\u6211\u4eec\u4e4b\u524d\u7684\u5f00\u53d1\uff0c\u6bcf\u5f53\u7528\u6237\u5411\u670d\u52a1\u5668\u53d1\u9001\u8bf7\u6c42\uff0c<strong><em>*\u54ea\u6015\u53ea\u662f\u9700\u8981\u66f4\u65b0\u4e00\u70b9\u70b9\u7684\u5c40\u90e8\u5185\u5bb9\uff0c\u670d\u52a1\u5668\u90fd\u4f1a\u5c06\u6574\u4e2a\u9875\u9762\u8fdb\u884c\u5237\u65b0\uff0c\u8fd9\u4e48\u505a\u7684\u95ee\u9898\u6709\u4e24\u70b9*<\/em><\/strong><\/p>\n<p><strong>\u2022<\/strong> <strong><em>*1\u3001\u6027\u80fd\u4f1a\u6709\u6240\u964d\u4f4e(\u4e00\u70b9\u5185\u5bb9\uff0c\u5237\u65b0\u6574\u4e2a\u9875\u9762\uff01)*<\/em><\/strong><\/p>\n<p><strong>\u2022<\/strong> <strong><em>*2\u3001\u7528\u6237\u7684\u64cd\u4f5c\u9875\u9762\u4f1a\u4e2d\u65ad(\u6574\u4e2a\u9875\u9762\u88ab\u5237\u65b0\u4e86)*<\/em><\/strong><\/p>\n<p>\u800c\u6211\u4eec\u57fa\u4e8eajax\u53ef\u4ee5\u4f7f\u7528Javascript\u6280\u672f\u5411\u670d\u52a1\u5668\u53d1\u9001\u5f02\u6b65\u8bf7\u6c42\uff0c\u56e0\u4e3a\u5f02\u6b65\u8bf7\u6c42\uff0c\u8fd9\u53ef\u4ee5\u4f7f\u6211\u4eec\u5728\u4e0d\u5237\u65b0\u9875\u9762\u7684\u524d\u63d0\u4e0b\u62ff\u5230\u540e\u7aef\u6570\u636e\uff0c\u5b8c\u6210\u9875\u9762\u7684\u5c40\u90e8\u5237\u65b0\uff0c\u6240\u4ee5\u603b\u7ed3\u4e3a\u4f55\u7528ajax\u5462\uff0c\u7262\u7262\u8bb0\u4f4f\u4e24\u4e2a\u5173\u952e\u8bcd\u5373\u53ef<\/p>\n<p>\u2022 1\u3001\u5f02\u6b65\u8bf7\u6c42<\/p>\n<p>\u2022 2\u3001\u5c40\u90e8\u5237\u65b0 <\/p>\n<p>\u4f8b\u5982\uff1a\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u4e00\u4e0b\u535a\u5ba2\u56ed\u7684\u6ce8\u518c\u9875\u9762\uff0c\u90a3\u4e9b\u5c40\u90e8\u5237\u65b0\u7684\u90e8\u5206\u80cc\u540e\u90fd\u662fajax\u8bf7\u6c42<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/wps2-1.jpg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/wps2-1.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<h3>3\u3001ajax\u7684\u539f\u7406<\/h3>\n<p><strong><em>\\<\/em>XMLHttpRequest\u5bf9\u8c61\u662fAjax\u4e2d\u6700\u91cd\u8981\u7684\u4e00\u4e2a\u5bf9\u8c61***<em>\u3002<\/em><\/strong>*\u4f7f\u7528Ajax\u66f4\u591a\u7684\u662f\u7f16\u5199\u5ba2\u6237\u7aef\u4ee3\u7801****\uff0c\u800c\u4e0d\u662f\u670d\u52a1\u7aef\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u6211\u4eec\u4f7f\u7528AJAX\u4e4b\u540e\uff0c<strong><em>*\u6d4f\u89c8\u5668\u662f\u5148\u628a\u8bf7\u6c42\u53d1\u9001\u5230XMLHttpRequest\u5f02\u6b65\u5bf9\u8c61\u4e4b\u4e2d\uff0c\u5f02\u6b65\u5bf9\u8c61\u5bf9\u8bf7\u6c42\u8fdb\u884c\u5c01\u88c5\uff0c\u7136\u540e\u518d\u4e0e\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u3002\u670d\u52a1\u5668\u5e76\u4e0d\u662f\u4ee5\u8f6c\u53d1\u7684\u65b9\u5f0f\u54cd\u5e94\uff0c\u800c\u662f\u4ee5\u6d41\u7684\u65b9\u5f0f\u628a\u6570\u636e\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668*<\/em><\/strong><\/p>\n<p>XMLHttpRequest\u5f02\u6b65\u5bf9\u8c61\u4f1a<strong><em>*\u4e0d\u505c\u76d1\u542c\u670d\u52a1\u5668\u72b6\u6001\u7684\u53d8\u5316\uff0c\u5f97\u5230\u670d\u52a1\u5668\u8fd4\u56de\u7684\u6570\u636e\uff0c\u5c31\u5199\u5230\u6d4f\u89c8\u5668\u4e0a\u3010\u56e0\u4e3a\u4e0d\u662f\u8f6c\u53d1\u7684\u65b9\u5f0f\uff0c\u6240\u4ee5\u662f\u65e0\u5237\u65b0\u5c31\u80fd\u591f\u83b7\u53d6\u670d\u52a1\u5668\u7aef\u7684\u6570\u636e\u3011*<\/em><\/strong><\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/wps3-1.jpg'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/wps3-1.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<h2>\u4e8c\u3001\u57fa\u4e8ejquery\u5b9e\u73b0ajax<\/h2>\n<p>\u6ce8\u610f<\/p>\n<pre><code class=\"language-python\">\u5982\u679c\u8bf7\u6c42\u65b9\u6cd5\u662fpost\uff0c\u9700\u8981\u5148\u6ce8\u91ca\u6389settings.py\u4e2d\u7684\u4e00\u884c\u4ee3\u7801\uff0c\u540e\u7eed\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u8be5\u884c\u4ee3\u7801\u7684\u4f5c\u7528\nMIDDLEWARE = [\n    ...\n    # &#039;django.middleware.csrf.CsrfViewMiddleware&#039;,\n    ...\n]<\/code><\/pre>\n<h3>1\u3001\u6848\u4f8b\u4e00\uff1a\u8ba1\u7b97\u5668<\/h3>\n<p>\u901a\u8fc7Ajax\uff0c\u5b9e\u73b0\u524d\u7aef\u8f93\u5165\u4e24\u4e2a\u6570\u5b57\uff0c\u670d\u52a1\u5668\u505a\u52a0\u6cd5\uff0c\u8fd4\u56de\u5230\u524d\u7aef\u9875\u9762\uff0c\u5b9e\u73b0\u5982\u4e0b<\/p>\n<p>index.html<\/p>\n<pre><code class=\"language-python\">&lt;input type=&quot;text&quot; id=&quot;num1&quot;&gt;+&lt;input type=&quot;text&quot; id=&quot;num2&quot;&gt;=&lt;input type=&quot;text&quot; id=&quot;sum&quot;&gt;\n&lt;button id=&quot;submit&quot;&gt;\u6211\u662f\u6309\u94ae&lt;\/button&gt;\n\n&lt;script src=&quot;https:\/\/code.jquery.com\/jquery-3.6.0.js&quot;&gt;&lt;\/script&gt;\n&lt;script&gt;\n    $(&quot;#submit&quot;).click(function () {\n        $.ajax({\n            url:&quot;\/test_ajax\/&quot;, \/\/ \u671d\u7740\u54ea\u4e2a\u5730\u5740\u53d1\u9001ajax\u8bf7\u6c42\uff0c\u4e0d\u5199\u5219\u9ed8\u8ba4\u5f53\u524d\u5730\u5740\n            type:&quot;post&quot;,  \/\/ \u8bf7\u6c42\u65b9\u6cd5\u53ef\u4ee5\u662fget\u6216\u8005post\n            data:{\n                n1:$(&quot;#num1&quot;).val(),\n                n2:$(&quot;#num2&quot;).val()\n            },  \/\/ \u671d\u540e\u7aef\u53d1\u9001\u7684\u6570\u636e\n            success:function (data) {\n                console.log(data);\n                $(&quot;#sum&quot;).val(data)\n            }\n        })\n    })\n&lt;\/script&gt;<\/code><\/pre>\n<p>urls.py<\/p>\n<pre><code class=\"language-python\">urlpatterns = [\n    path(&#039;index\/&#039;,views.index),\n    path(&#039;test_ajax\/&#039;,views.test_ajax),\n]<\/code><\/pre>\n<p>views.py<\/p>\n<pre><code class=\"language-python\">def index(request):\n    return render(request, &#039;index.html&#039;)\n\ndef test_ajax(request):\n    request.is_ajax()\n    num1 = request.POST.get(&quot;n1&quot;)\n    num2 = request.POST.get(&quot;n2&quot;)\n    res = int(num1) + int(num2)\n    return HttpResponse(res)<\/code><\/pre>\n<p><strong><em>*Ajax\u2014-&gt;\u670d\u52a1\u5668\u2014\u2014&gt;Ajax\u6267\u884c\u6d41\u7a0b\u56fe*<\/em><\/strong><\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/wps4.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/wps4.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<h3>2\u3001\u6848\u4f8b\u4e8c\uff1a\u767b\u5f55\u9a8c\u8bc1<\/h3>\n<p>\u7528\u6237\u5728\u8868\u5355\u8f93\u5165\u7528\u6237\u540d\u4e0e\u5bc6\u7801\uff0c\u901a\u8fc7Ajax\u63d0\u4ea4\u7ed9\u670d\u52a1\u5668\uff0c\u670d\u52a1\u5668\u9a8c\u8bc1\u540e\u8fd4\u56de\u54cd\u5e94\u4fe1\u606f\uff0c\u5ba2\u6237\u7aef\u901a\u8fc7\u54cd\u5e94\u4fe1\u606f\u786e\u5b9a\u662f\u5426\u767b\u5f55\u6210\u529f\uff0c\u6210\u529f\uff0c\u5219\u8df3\u8f6c\u5230\u9996\u9875\uff0c\u5426\u5219\uff0c\u5728\u9875\u9762\u4e0a\u663e\u793a\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f<\/p>\n<p>login.html<\/p>\n<pre><code class=\"language-python\">\u7528\u6237\u540d\uff1a&lt;input type=&quot;text&quot; id=&quot;user&quot;&gt;\n\u5bc6\u7801\uff1a&lt;input type=&quot;text&quot; id=&quot;pwd&quot;&gt;\n&lt;button id=&quot;btn&quot;&gt;\u767b\u5f55&lt;\/button&gt;&lt;span id=&quot;err&quot;&gt;&lt;\/span&gt;\n&lt;script src=&quot;https:\/\/code.jquery.com\/jquery-3.6.0.js&quot;&gt;&lt;\/script&gt;\n&lt;script&gt;\n    $(&quot;#btn&quot;).click(function(){\n        $.ajax({\n            url:&quot;&quot;,\n            type:&quot;post&quot;,\n            data:{\n                username:$(&quot;#user&quot;).val(),\n                password:$(&quot;#pwd&quot;).val(),\n            },\n            success:function(data){\n                var data = JSON.parse(data);\n                if (data.user) {\n                    location.href=&quot;\/index\/&quot;;\n                }else {\n                    $(&quot;#err&quot;).text(data.message).css(&quot;color&quot;,&quot;red&quot;)\n                }\n            }\n        })\n    })\n&lt;\/script&gt;<\/code><\/pre>\n<p>urls.py<\/p>\n<pre><code class=\"language-python\">urlpatterns = [\n    path(&#039;index\/&#039;,views.index),\n    path(&#039;login\/&#039;,views.login),\n]<\/code><\/pre>\n<p>views.py<\/p>\n<pre><code class=\"language-python\">def login(request):\n    if request.method == &quot;GET&quot;:\n        return render(request, &#039;login.html&#039;)\n    elif request.method == &quot;POST&quot;:\n        username = request.POST.get(&#039;username&#039;)\n        password = request.POST.get(&#039;password&#039;)\n\n        back_dic = {&#039;user&#039;: None, &#039;message&#039;: None}\n        if username == &quot;egon&quot; and password == &quot;123&quot;:\n            back_dic[&#039;user&#039;] = username\n            back_dic[&#039;message&#039;] = &quot;\u767b\u5f55\u6210\u529f&quot;\n        else:\n            back_dic[&#039;message&#039;]=&#039;\u7528\u6237\u540d\u6216\u5bc6\u7801\u9519\u8bef&#039;\n\n        import json\n        return HttpResponse(json.dumps(back_dic))<\/code><\/pre>\n<h2>\u4e09\u3001\u6587\u4ef6\u4e0a\u4f20<\/h2>\n<h3>1\u3001\u50a8\u5907\u77e5\u8bc6HttpRequest.body<\/h3>\n<pre><code class=\"language-python\">\u524d\u7aef\u7684\u8bf7\u6c42\u53d1\u9001\u5230django\u540e\u90fd\u4f1a\u88ab\u5c01\u88c5\u5230HttpRequest\u5bf9\u8c61\u4e2d\uff0c\u800c\u8be5\u5bf9\u8c61\u4e0b\u6709\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5c5e\u6027\nHttpRequest.body\uff0c\u5373\u8bf7\u6c42\u4f53\n\n# \u4e00 \u5f53\u8bf7\u6c42\u65b9\u6cd5\u4e3aGET\n\u5f53\u6d4f\u89c8\u5668\u57fa\u4e8ehttp\u534f\u8bae\u7684GET\u65b9\u6cd5\u63d0\u4ea4\u6570\u636e\u65f6\uff0c\u6ca1\u6709\u8bf7\u6c42\u4f53\u4e00\u8bf4,\n\u6570\u636e\u4f1a\u6309\u7167k1=v1&amp;k2=v2&amp;k3=v3\u7684\u683c\u5f0f\u653e\u5230url\u4e2d\uff0c\n\u7136\u540e\u53d1\u9001\u7ed9django\uff0cdjango\u4f1a\u5c06\u8fd9\u4e9b\u6570\u636e\u5c01\u88c5\u5230request.GET\u4e2d\uff0c\n\u6ce8\u610f\u6b64\u65f6\u7684\u8bf7\u6c42\u4f53request.body\u4e3a\u7a7a\u3001\u65e0\u7528\n\n# \u4e8c \u5f53\u8bf7\u6c42\u65b9\u6cd5\u4e3aPOST\u65f6\n\u5f53\u6d4f\u89c8\u5668\u57fa\u4e8ehttp\u534f\u8bae\u7684POST\u65b9\u6cd5\u63d0\u4ea4\u6570\u636e\u65f6\uff0c\u6570\u636e\u4f1a\u653e\u5728\u8bf7\u6c42\u4f53\u5185\uff0c\u7136\u540e\u53d1\u9001\u7ed9django\ndjango\u4f1a\u5c06\u63a5\u6536\u5230\u7684\u8bf7\u6c42\u4f53\u6570\u636e\u5b58\u653e\u4e8eHttpRequest.body\u5c5e\u6027\u4e2d.\n\u4f46\u8be5\u5c5e\u6027\u7684\u503c\u4e3aBytes\u7c7b\u578b(\u5957\u63a5\u5b57\u6570\u636e\u4f20\u8f93\u90fd\u662fbytes\u7c7b\u578b)\uff0c\n\u800c\u901a\u5e38\u60c5\u51b5\u4e0b\u76f4\u63a5\u5904\u7406Bytes\u3001\u5e76\u4ece\u4e2d\u63d0\u53d6\u6709\u7528\u6570\u636e\u7684\u64cd\u4f5c\u662f\u590d\u6742\u800c\u7e41\u7410\u7684\uff0c\n\u597d\u5728django\u4f1a\u5bf9\u5b83\u505a\u8fdb\u4e00\u6b65\u7684\u5904\u7406\u4e0e\u5c01\u88c5\u4ee5\u4fbf\u6211\u4eec\u66f4\u4e3a\u65b9\u4fbf\u5730\u63d0\u53d6\u6570\u636e\uff0c\n\u5177\u4f53\u5982\u4f55\u5904\u7406\u5462\uff1f\n\n\u5f53\u524d\u7aef\u91c7\u7528POST\u63d0\u4ea4\u6570\u636e\u65f6\uff0c\u6570\u636e\u6709\u4e09\u79cd\u5e38\u7528\u7f16\u7801\u683c\u5f0f\uff0c\u7f16\u7801\u683c\u5f0f\u4e0d\u540cDjango\u4f1a\u6709\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\n  # \u7f16\u7801\u683c\u5f0f1\uff1aapplication\/x-www-form-urlencoded\uff0c\u662fform\u8868\u5355\u9ed8\u8ba4\u7f16\u7801\u683c\u5f0f\n  # \u7f16\u7801\u683c\u5f0f2\uff1amultipart\/form-data\uff0c\u4e0a\u4f20\u6587\u4ef6\u4e13\u7528\u683c\u5f0f\n  # \u7f16\u7801\u683c\u5f0f2\uff1aapplication\/json\uff0c\u63d0\u4ea4jason\u683c\u5f0f\u5b57\u7b26\u4e32\n\n  #====\u300bPOST\u8bf7\u6c42\u4f53\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f1\uff1aapplication\/x-www-form-urlencoded\u65f6\u300a====\n      HttpRequest.body\u4e2d\u7684\u6570\u636e\u683c\u5f0f\u4e3ab&#039;a=1&amp;b=2&amp;c=3&#039;\n      django\u4f1a\u5c06\u5176\u63d0\u53d6\u51fa\u6765\u5c01\u88c5\u5230request.POST\u4e2d\n      request.FILES\u6b64\u65f6\u4e3a\u7a7a\n\n      \u5982\uff1a\n      print(request.body) # b&#039;a=1&amp;b=2&amp;c=3&#039;\n      print(request.POST) # &lt;QueryDict: {&#039;a&#039;: [&#039;1&#039;],&#039;b&#039;: [&#039;2&#039;],&#039;c&#039;: [&#039;3&#039;]}\n      print(request.FILES) # &lt;MultiValueDict: {}&gt;\n\n  #====\u300bPOST\u8bf7\u6c42\u4f53\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f2\uff1amultipart\/form-data\u65f6\u300a====      \n      HttpRequest.body\u4e2d\u7684\u6570\u636e\u683c\u5f0f\u4e3ab&#039;------WebKitFormBoundaryKtcwuksQltpNprep\\r\\nContent-Disposition: form-data;......&#039;,\u6ce8\u610f\uff0c\u6587\u4ef6\u90e8\u5206\u6570\u636e\u662f\u6d41\u6570\u636e\uff0c\u6240\u4ee5\u4e0d\u5728\u6d4f\u89c8\u5668\u4e2d\u663e\u793a\u662f\u6b63\u5e38\u7684\n      django\u4f1a\u5c06request.body\u4e2d\u7684\u975e\u6587\u4ef6\u6570\u636e\u90e8\u5206\u63d0\u53d6\u51fa\u6765\u5c01\u88c5\u5230request.POST\u4e2d\n      \u5c06\u4e0a\u4f20\u7684\u6587\u4ef6\u90e8\u5206\u6570\u636e\u4e13\u95e8\u63d0\u53d6\u51fa\u6765\u5c01\u88c5\u5230request.FILES\u5c5e\u6027\u4e2d\n\n      \u5982\uff1a\n      print(request.body) # \u4e0d\u8981\u6253\u5370\u5b83\uff0c\u6253\u5370\u5219\u62a5\u9519\uff0c\u56e0\u4e3a\u5b83\u662f\u6570\u636e\u6d41\n      print(request.POST) # &lt;QueryDict: {&#039;a&#039;: [&#039;1&#039;],&#039;b&#039;: [&#039;2&#039;],&#039;c&#039;: [&#039;3&#039;]}\n      print(request.FILES) # &lt;MultiValueDict: {&#039;head_img&#039;: [&lt;InMemoryUploadedFile: 11111.jpg (image\/jpeg)&gt;]}&gt;\n\n      \u5f3a\u8c03\uff1a\n        1\u3001\u6beb\u65e0\u7591\u95ee\uff0c\u7f16\u7801\u683c\u5f0f2\u7684\u6570\u636e\u91cf\u8981\u5927\u4e8e\u7f16\u7801\u683c\u5f0f1\uff0c\u5982\u679c\u65e0\u9700\u4e0a\u4f20\u6587\u4ef6\uff0c\u8fd8\u662f\u63a8\u8350\u4f7f\u7528\u66f4\u4e3a\u7cbe\u7b80\u7684\u7f16\u7801\u683c\u5f0f1\n        2\u3001FILES will only contain data if the request method was POST and the &lt;form&gt; that posted to the request had enctype=&quot;multipart\/form-data&quot;. Otherwise, FILES will be a blank dictionary-like object.\n\n  #===\u300bPOST\u8bf7\u6c42\u4f53\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f3\uff1aapplication\/json\u65f6\u300a====\n      \u6b64\u65f6\u5728django\u540e\u53f0\uff0crequest.POST\u548crequest.FILES\u4e2d\u662f\u6ca1\u6709\u503c\u7684\uff0c\u90fd\u653e\u5230request.body\u4e2d\u4e86\uff0c\u9700\u8981\u7528json.loads\u5bf9\u5176\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\n\n      \u5982\uff1a\n      print(request.body) # b&#039;{&quot;a&quot;:1,&quot;b&quot;:2,&quot;c&quot;:3}&#039;\n      print(request.POST) # &lt;QueryDict: {}&gt;\n      print(request.FILES) # &lt;MultiValueDict: {}&gt;\n\n# \u5982\u4f55\u8bbe\u5b9aPOST\u63d0\u4ea4\u6570\u636e\u7684\u7f16\u7801\u683c\u5f0f \n    \u524d\u7aef\u5f80\u540e\u53f0POST\u63d0\u4ea4\u6570\u636e\uff0c\u5e38\u7528\u6280\u672f\u6709form\u8868\u5355\u548cajax\u4e24\u79cd\n    form\u8868\u5355\u53ef\u4ee5\u8bbe\u7f6e\u7684\u6570\u636e\u7f16\u7801\u683c\u5f0f\u6709\uff1a\u7f16\u7801\u683c\u5f0f1\u3001\u7f16\u7801\u683c\u5f0f2\n    ajax\u53ef\u4ee5\u8bbe\u7f6e\u7684\u6570\u636e\u7f16\u7801\u683c\u5f0f\u6709\uff1a\u7f16\u7801\u683c\u5f0f1\u3001\u7f16\u7801\u683c\u5f0f2\u3001\u7f16\u7801\u683c\u5f0f3   <\/code><\/pre>\n<h3>2\u3001form\u8868\u5355\u4e0a\u4f20\u6587\u4ef6<\/h3>\n<p>form\u8868\u5355\u53ef\u4ee5\u901a\u8fc7\u5c5e\u6027enctype\u8fdb\u884c\u8bbe\u7f6e\u7f16\u7801\u683c\uff0c\u5982\u4e0b<\/p>\n<p>\u7f16\u7801\u683c\u5f0f1(\u9ed8\u8ba4\u7684\u7f16\u7801\u683c\u5f0f):enctype=&quot;application\/x-www-form-urlencoded&quot; <em><br \/>\n<\/em>\u7f16\u7801\u683c\u5f0f2(\u4f7f\u7528form\u8868\u5355\u4e0a\u4f20\u6587\u4ef6\u65f6\u53ea\u80fd\u7528\u8be5\u7f16\u7801):enctype=&quot;multipart\/form-data&quot; <\/p>\n<p>\u9488\u5bf9\u7f16\u7801\u683c\u5f0f2\u6211\u4eec\u6765\u5199\u4e00\u4e2aform\u8868\u5355\u4e0a\u4f20\u6587\u4ef6\u7684\u6848\u4f8b\uff0c\u5982\u4e0b<\/p>\n<p>\u5728templates\u76ee\u5f55\u4e0b\u65b0\u5efaregister.html<\/p>\n<pre><code class=\"language-python\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;\u6ce8\u518c\u9875\u9762&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n&lt;form action=&quot;&quot; method=&quot;POST&quot; enctype=&quot;multipart\/form-data&quot; &gt;\n    {% csrf_token %}\n    &lt;p&gt;\n        \u7528\u6237\u540d:&lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;\n    &lt;\/p&gt;\n    &lt;p&gt;\n        \u5934\u50cf:&lt;input type=&quot;file&quot; name=&quot;header_img&quot;&gt;\n    &lt;\/p&gt;\n    &lt;p&gt;\n        &lt;input type=&quot;submit&quot; value=&quot;\u63d0\u4ea4&quot;&gt;\n    &lt;\/p&gt;\n&lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>urls.py<\/p>\n<pre><code class=\"language-python\">from django.urls import re_path\nfrom app01 import views\n\nurlpatterns = [\n    re_path(r&#039;^register\/$&#039;,views.register),\n]<\/code><\/pre>\n<p>views.py<\/p>\n<pre><code class=\"language-python\">from django.shortcuts import render,HttpResponse\n\ndef register(request):\n    &#039;&#039;&#039;\n    \u4fdd\u5b58\u4e0a\u4f20\u6587\u4ef6\u524d\uff0c\u6570\u636e\u9700\u8981\u5b58\u653e\u5728\u67d0\u4e2a\u4f4d\u7f6e\u3002\u9ed8\u8ba4\u5f53\u4e0a\u4f20\u6587\u4ef6\u5c0f\u4e8e2.5M\u65f6\uff0cdjango\u4f1a\u5c06\u4e0a\u4f20\u6587\u4ef6\u7684\u5168\u90e8\u5185\u5bb9\u8bfb\u8fdb\u5185\u5b58\u3002\u4ece\u5185\u5b58\u8bfb\u53d6\u4e00\u6b21\uff0c\u5199\u78c1\u76d8\u4e00\u6b21\u3002\n    \u4f46\u5f53\u4e0a\u4f20\u6587\u4ef6\u5f88\u5927\u65f6\uff0cdjango\u4f1a\u628a\u4e0a\u4f20\u6587\u4ef6\u5199\u5230\u4e34\u65f6\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5b58\u653e\u5230\u7cfb\u7edf\u4e34\u65f6\u6587\u4ef6\u5939\u4e2d\u3002\n    &#039;&#039;&#039;\n    if request.method == &#039;GET&#039;:\n        return render(request, &#039;register.html&#039;)\n    elif request.method == &#039;POST&#039;:\n        # print(request.body) \n\n        # \u4ecerequest.POST\u4e2d\u83b7\u53d6\u7528\u6237\u540d\n        name = request.POST.get(&#039;name&#039;)\n\n        # \u4ecerequest.FILES\u83b7\u53d6\u6587\u4ef6\u5bf9\u8c61\n        file_obj = request.FILES.get(&#039;header_img&#039;)\n        # \u4ece\u6587\u4ef6\u5bf9\u8c61\u4e2d\u83b7\u53d6\u6587\u4ef6\u540d\n        filename = file_obj.name\n        print(filename)\n\n        file_obj.chunks()\n        # \u4e0a\u4f20\u7684\u6587\u4ef6\u5b58\u653e\u4e8etemplates\u6587\u4ef6\u5939\u4e0b\n        with open(&#039;templates\/%s&#039; %filename, &#039;wb&#039;) as f:\n            for line in file_obj.chunks():\n                f.write(line)\n\n        return HttpResponse(&#039;\u6ce8\u518c\u6210\u529f&#039;)\n\n    # \u5f3a\u8c03\uff1a\n    &#039;&#039;&#039;\n    file_obj.read() \u5f53\u6587\u4ef6\u8fc7\u5927\u65f6\uff0c\u4f1a\u5bfc\u81f4\u5185\u5b58\u6ea2\u51fa\uff0c\u7cfb\u7edf\u5d29\u6e83\n    \u5e94\u8be5\u4f7f\u7528file_obj.chunks()\u8bfb\u53d6\uff0c\u5b98\u7f51\u89e3\u91ca\u5982\u4e0b\n    uploadedFile.chunks(chunk_size=None)\u00b6\n    A generator returning chunks of the file. If multiple_chunks() is True, you should use this method in a loop instead of read().\n\n    In practice, it\u2019s often easiest simply to use chunks() all the time. Looping over chunks() instead of using read() ensures that large files don\u2019t overwhelm your system\u2019s memory.\n\n    \u6ce8\u610f\uff1a\u76f4\u63a5for\u5faa\u73affile_obj\u4e5f\u4e0d\u884c\uff0c\u56e0\u4e3a\u76f4\u63a5for\u8bfb\u53d6\u6587\u4ef6\u662f\u6309\u7167\u884c\u5206\u9694\u7b26\u6765\u4f9d\u6b21\u8bfb\u53d6\u7684\uff0c\u4e0d\u540c\u5e73\u53f0\u4e0b\u7684\u6587\u4ef6\u884c\u5206\u9694\u7b26\u4e0d\u540c\uff0c\u6709\u53ef\u80fd\u4e00\u884c\u7684\u6570\u636e\u5c31\u5f88\u5927\uff0c\u6240\u4ee5\u8fd8\u662f\u63a8\u8350file_obj.chunks()\u6765\u8bfb\u53d6\uff0c\u5b98\u7f51\u89e3\u91ca\u5982\u4e0b\n    Like regular Python files, you can read the file line-by-line simply by iterating over the uploaded file:\n\n    for line in uploadedfile:\n        do_something_with(line)\n\n    Lines are split using universal newlines. The following are recognized as ending a line: the Unix end-of-line convention &#039;\\n&#039;, the Windows convention &#039;\\r\\n&#039;, and the old Macintosh convention &#039;\\r&#039;.\n    &#039;&#039;&#039;<\/code><\/pre>\n<h3>3\u3001ajax\u4e0a\u4f20\u6587\u4ef6<\/h3>\n<p>ajax\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u5982\u4e0b<\/p>\n<pre><code class=\"language-python\"># 1\u3001\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u4e3a\uff1aapplication\/x-www-form-urlencoded\ncontentType:&quot;application\/x-www-form-urlencoded&quot;  \/\/ \u4e0d\u6307\u5b9a\u9ed8\u8ba4\u5c31\u662f\u8fd9\u79cd\u683c\u5f0f\n\n# 2\u3001\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u4e3a\uff1amultipart\/form-data\uff0c\u4e0d\u662f\u76f4\u63a5\u6307\u5b9a\uff0c\u800c\u662f\u901a\u8fc7\u4e0b\u8ff0\u65b9\u5f0f\n    var formdata=new FormData();  \/\/ \u521b\u5efa\u4e00\u4e2aformdata\u5bf9\u8c61\n    formdata.append(&#039;user&#039;,$(&quot;#user&quot;).val());  \n    formdata.append(&#039;img&#039;,$(&quot;#img&quot;)[0].files[0]);\n    $.ajax({\n        url:&#039;&#039;,\n        type:&#039;post&#039;,\n        processData:false, \/\/\u544a\u8bc9jQuery\u4e0d\u8981\u53bb\u5904\u7406\u53d1\u9001\u7684\u6570\u636e\n        contentType:false,\/\/ \u544a\u8bc9jQuery\u4e0d\u8981\u53bb\u8bbe\u7f6eContent-Type\u8bf7\u6c42\u5934\n        data:formdata,\n        success:function (data) {\n            console.log(data)\n        }\n    })\n\n# 3\u3001\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u4e3a\uff1aapplication\/json\ncontentType:&#039;application\/json&#039;\ndata:JSON.stringify(\u4f60\u7684\u6570\u636e),  \/\/ \u8bb0\u4f4f\u5c06\u6570\u636e\u8f6c\u6362\u6210json\u5b57\u7b26\u4e32\u683c\u5f0f<\/code><\/pre>\n<p>\u9488\u5bf9\u7f16\u7801\u683c\u5f0f2\u6211\u4eec\u6765\u5199\u4e00\u4e2aajax\u4e0a\u4f20\u6587\u4ef6\u7684\u6848\u4f8b\uff0c\u5982\u4e0b<\/p>\n<p>\u5728templates\u76ee\u5f55\u4e0b\u65b0\u5efaregister.html<\/p>\n<pre><code class=\"language-python\">&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;\u6ce8\u518c\u9875\u9762&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n\n\u7528\u6237\u540d:&lt;input type=&quot;text&quot; id=&quot;user&quot;&gt;\n\u5934\u50cf:&lt;input type=&quot;file&quot; id=&quot;img&quot;&gt;\n&lt;button id=&quot;btn&quot;&gt;\u63d0\u4ea4&lt;\/button&gt;\n&lt;script src=&quot;https:\/\/code.jquery.com\/jquery-3.6.0.js&quot;&gt;&lt;\/script&gt;\n&lt;script&gt;\n    $(&quot;#btn&quot;).click(function () {\n        var formdata=new FormData();  \/\/ \u521b\u5efa\u4e00\u4e2aformdata\u5bf9\u8c61\n        formdata.append(&#039;user&#039;,$(&quot;#user&quot;).val());\n        formdata.append(&#039;img&#039;,$(&quot;#img&quot;)[0].files[0]);\n        $.ajax({\n            url:&quot;&quot;,\n            type:&quot;post&quot;,\n            data: formdata,\n            processData: false,\n            contentType:false,\n            success:function (data) {\n                console.log(data);\n            }\n        })\n    })\n&lt;\/script&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>urls.py<\/p>\n<pre><code class=\"language-python\">from django.urls import re_path\nfrom app01 import views\n\nurlpatterns = [\n    re_path(r&#039;^register\/$&#039;,views.register),\n]<\/code><\/pre>\n<p>views.py<\/p>\n<pre><code class=\"language-python\">def register(request):\n    if request.method == &quot;POST&quot;:\n        print(&#039;==ajax==&gt;&#039;)\n        # print(request.body)  # \u6253\u5370\u6709\u53ef\u80fd\u4f1a\u62a5\u9519\n        print(request.POST)\n        print(request.FILES)\n        file_obj = request.FILES.get(&quot;img&quot;)\n        filename = file_obj.name\n        with open(&#039;templates\/%s&#039; % filename, mode=&#039;wb&#039;) as f:\n            for line in file_obj.chunks():\n                f.write(line)\n        return HttpResponse(&quot;\u4e0a\u4f20\u5b8c\u6bd5&quot;) \n    return render(request, &#039;register.html&#039;)<\/code><\/pre>\n<h3>4\u3001ajax\u53d1\u9001json\u6570\u636e<\/h3>\n<p>register.html<\/p>\n<pre><code class=\"language-python\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;\u6ce8\u518c\u9875\u9762&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;button id=&quot;btn&quot;&gt;\u63d0\u4ea4&lt;\/button&gt;\n&lt;script src=&quot;https:\/\/code.jquery.com\/jquery-3.6.0.js&quot;&gt;&lt;\/script&gt;\n&lt;script&gt;\n    $(&quot;#btn&quot;).click(function () {\n        var dic = {&quot;name&quot;:&quot;egon&quot;,&quot;age&quot;:18};\n        $.ajax({\n            url:&quot;&quot;,\n            type:&quot;post&quot;,\n            contentType:&quot;application\/json&quot;,\n            data: JSON.stringify(dic),\n            success:function (data) {\n                var res = JSON.parse(data);  \/\/ \u5982\u679c\u8fd4\u56de\u7684\u6570\u636e\u4e5f\u662fjson\u683c\u5f0f\uff0c\u90a3\u4e48\u9700\u8981\u5148\u53cd\u5e8f\u5217\u5316\n                console.log(res.x);  \/\/ \u7136\u540e\u624d\u80fd\u8bbf\u95ee\u5c5e\u6027\n\n                \/\/ console.log(data.x);  \/\/ \u5982\u679c\u540e\u53f0\u8fd4\u56de\u7684\u662fJsonResponse,\u90a3\u4e48\u76f4\u63a5\u4f7f\u7528\u5c31\u597d\n            }\n        })\n    })\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>urls.py<\/p>\n<pre><code class=\"language-python\">urlpatterns = [\n    re_path(&quot;^register\/$&quot;,views.register)\n]<\/code><\/pre>\n<p>views.py<\/p>\n<pre><code class=\"language-python\">def register(request):\n    if request.method == &quot;POST&quot;:\n        print(&#039;==ajax==&gt;&#039;)\n        print(request.is_ajax())  # True\n        print(request.body)  # b&#039;{&quot;name&quot;:&quot;egon&quot;,&quot;age&quot;:18}&#039;\n        print(request.POST)  # &lt;QueryDict: {}&gt;\n        print(request.FILES)  # &lt;MultiValueDict: {}&gt;\n\n        import json\n        dic = json.loads(request.body)\n        print(dic[&quot;name&quot;])\n\n        # from django.http import JsonResponse\n        # return JsonResponse({&quot;x&quot;:1,&quot;y&quot;:2})  # \u53ef\u4ee5\u76f4\u63a5\u8fd4\u56deJsonRResponse\u524d\u7aef\u5c31\u4e0d\u9700\u8981\u53cd\u5e8f\u5217\u5316\u4e86\n\n        return HttpResponse(json.dumps({&quot;x&quot;:1,&#039;y&#039;:2}))  # \u524d\u7aef\u9700\u8981\u53cd\u5e8f\u5217\u5316\u624d\u53ef\u4ee5\u4f7f\u7528\n    return render(request, &#039;register.html&#039;)<\/code><\/pre>\n<h3>5\u3001Django\u5185\u7f6e\u7684serializers<\/h3>\n<p>\u5982\u679c\u6211\u7684\u524d\u7aef\u60f3\u62ff\u5230\u7531ORM\u5f97\u5230\u7684\u6570\u636e\u5e93\u91cc\u9762\u7684\u4e00\u4e2a\u4e2a\u7528\u6237\u5bf9\u8c61\uff0c\u6211\u7684\u540e\u7aef\u60f3\u76f4\u63a5\u5c06\u5b9e\u4f8b\u5316\u51fa\u6765\u7684\u6570\u636e\u5bf9\u8c61\u76f4\u63a5\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u7528Django\u7ed9\u6211\u4eec\u63d0\u4f9b\u7684\u5e8f\u5217\u5316\u65b9\u5f0f<\/p>\n<pre><code class=\"language-python\">def register(request):\n    if request.method == &quot;POST&quot;:\n        from app01.models import Book\n        from django.core import serializers\n\n        objs = Book.objects.all()\n        res = serializers.serialize(&#039;json&#039;,objs)\n\n        return HttpResponse(res)\n    return render(request, &#039;register.html&#039;)<\/code><\/pre>\n<p>\u524d\u7aef\u4ee3\u7801<\/p>\n<pre><code class=\"language-python\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;\u6ce8\u518c\u9875\u9762&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;button id=&quot;btn&quot;&gt;\u63d0\u4ea4&lt;\/button&gt;\n&lt;script src=&quot;https:\/\/code.jquery.com\/jquery-3.6.0.js&quot;&gt;&lt;\/script&gt;\n&lt;script&gt;\n    $(&quot;#btn&quot;).click(function () {\n        var dic = {&quot;name&quot;:&quot;egon&quot;,&quot;age&quot;:18};\n        $.ajax({\n            url:&quot;&quot;,\n            type:&quot;post&quot;,\n            contentType:&quot;application\/json&quot;,\n            data: JSON.stringify(dic),\n            success:function (data) {\n                var books = JSON.parse(data);  \/\/ \u5982\u679c\u8fd4\u56de\u7684\u6570\u636e\u4e5f\u662fjson\u683c\u5f0f\uff0c\u90a3\u4e48\u9700\u8981\u53cd\u5e8f\u5217\u5316\n                books.forEach(function (book) {\n                    console.log(book.fields.name);\n                    console.log(book.fields.price);\n                    console.log(book.fields.publish_date);\n                });\n            }\n        })\n    })\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ajax \u4e00\u3001Ajax\u4ecb\u7ecd 1\u3001\u4ec0\u4e48\u662fajax \u7b80\u5355\u5730\u8bb2\uff0cajax\u5c31\u662f\u4e00\u79cd\u524d\u7aef\u6280\u672f\uff0c\u7528\u4e8e\u671d\u7740\u540e\u7aef\u7a0b\u5e8f\u53d1\u9001\u8bf7\u6c42\u548c [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":6800,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,405],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/6155"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6155"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/6155\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/6800"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}