{"id":1170,"date":"2024-12-28T14:39:50","date_gmt":"2024-12-28T06:39:50","guid":{"rendered":"http:\/\/www.chan.ink\/?p=1170"},"modified":"2024-12-28T21:48:04","modified_gmt":"2024-12-28T13:48:04","slug":"%e3%80%90djangocsrftoken%e3%80%91%e8%8e%b7%e5%8f%96csrftoken%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/www.chan.ink\/index.php\/2024\/12\/28\/%e3%80%90djangocsrftoken%e3%80%91%e8%8e%b7%e5%8f%96csrftoken%e7%9a%84%e5%87%bd%e6%95%b0\/","title":{"rendered":"\u3010Django|csrftoken\u3011\u83b7\u53d6csrftoken\u7684\u51fd\u6570"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u8be6\u89c1Django\u6e90\u7801\u5206\u6790\u73ed7.09\u7ea625\u5206\u949f\u524d\u540e\u5904<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getCookie(name) {\n    let cookieValue = null;\n    if (document.cookie &amp;&amp; document.cookie !== '') {\n        const cookies = document.cookie.split(';');\n        for (let i = 0; i &lt; cookies.length; i++) {\n            const cookie = cookies&#91;i].trim();\n            \/\/ Does this cookie string begin with the name we want?\n            if (cookie.substring(0, name.length + 1) === (name + '=')) {\n                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\n                break;\n            }\n        }\n    }\n    return cookieValue;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u4e8eAjax\u4e2d\u4f20\u9012csrftoken\u503c\u7684\u4e9b\u8bb8\u95ee\u9898\u8bb0\u5f55<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u603b\u7ed3\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.\u524d\u7aef\u4e2d\uff0c\u5c24\u5176\u662fajax\u4e2d\uff0c\u4f7f\u7528\u6a2a\u6760\uff0c\u800c\u975e\u4e0b\u5212\u7ebf<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2.\u89c6\u56fe\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528\u4e0b\u5212\u7ebf\uff0c\u800c\u975e\u6a2a\u6760\uff0c\u4e14\u5b57\u6bcd\u90fd\u5f97\u5927\u5199<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1.\u5728Ajax\u4e2d\uff0cheaders\u4e2d\uff0c\u63d0\u4ea4\u7684\u8bf7\u6c42\u5934\uff0c\u5fc5\u987b\u4f7f\u7528-\u4f5c\u4e3a\u5206\u9694\uff0c\u800c\u975e_<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1252\" height=\"571\" src=\"http:\/\/www.chan.ink\/wp-content\/uploads\/2024\/12\/1735367572591.png\" alt=\"\" class=\"wp-image-1171\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u5904\u5148\u4ee5\u9519\u8bef\u4f5c\u4e3a\u5c55\u793a\uff0c\u4f7f\u7528_<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6d4f\u89c8\u5668\u540e\u53f0\u53ef\u4ee5\u770b\u5904\u9519\u8bef\u4e4b\u5904<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1875\" height=\"601\" src=\"http:\/\/www.chan.ink\/wp-content\/uploads\/2024\/12\/1735367617620.png\" alt=\"\" class=\"wp-image-1172\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u89c6\u56fe\u51fd\u6570\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"279\" src=\"http:\/\/www.chan.ink\/wp-content\/uploads\/2024\/12\/1735367708567.png\" alt=\"\" class=\"wp-image-1174\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u770bdjango\u540e\u53f0\u7684\u65e5\u5fd7\u8bb0\u5f55<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"114\" src=\"http:\/\/www.chan.ink\/wp-content\/uploads\/2024\/12\/1735367658882.png\" alt=\"\" class=\"wp-image-1173\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2.\u4fee\u6539\u4e0b\u5212\u7ebf\u4e3a\u6a2a\u6760\uff0c\u5982\u4e0b\u56fe<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1228\" height=\"582\" src=\"http:\/\/www.chan.ink\/wp-content\/uploads\/2024\/12\/1735367727654.png\" alt=\"\" class=\"wp-image-1175\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u770b\u6d4f\u89c8\u5668\u540e\u53f0<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1879\" height=\"367\" src=\"http:\/\/www.chan.ink\/wp-content\/uploads\/2024\/12\/1735367800485.png\" alt=\"\" class=\"wp-image-1176\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u770bdjango\u540e\u53f0<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"813\" height=\"144\" src=\"http:\/\/www.chan.ink\/wp-content\/uploads\/2024\/12\/1735367823819.png\" alt=\"\" class=\"wp-image-1177\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">xxxx_123\u6ca1\u6709\u6570\u636e\uff0c\u662f\u56e0\u4e3a\uff0c\u5728ajax\u4e2d\uff0c\u4f7f\u7528\u7684\u662f\u4e0b\u5212\u7ebf\uff0c\u800c\u975e\u6a2a\u6760<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u6539\u540e\u5982\u4e0b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"3487\" height=\"1009\" src=\"http:\/\/www.chan.ink\/wp-content\/uploads\/2024\/12\/1735367908122.png\" alt=\"\" class=\"wp-image-1178\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u6bcf\u4e2aajax\u8bf7\u6c42\u90fd\u8fd9\u4e48\u5199\uff0c\u7565\u663e\u9ebb\u70e6\uff0c\u53ef\u4ee5\u7c7b\u4f3c\u4e8epython\u7684\u88c5\u9970\u5668\uff0c\u5199\u4e2a\u5728\u6bcf\u6b21ajax\u8bf7\u6c42\u53d1\u9001\u6570\u636e\u524d\u5fc5\u987b\u6267\u884c\u7684\u51fd\u6570\uff0c\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  function bindSendSmsEvent(){\n        $.ajaxSetup({\n            beforeSend:function (xhr,settings){\n                xhr.setRequestHeaders('X-CSRFTOKEN',getCookie('csrftoken'));\n            }\n        })\n    }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u7684\u51fd\u6570\uff0c\u4ecd\u6709\u4e0d\u8db3\uff0c\u56e0\u4e3aGET\u4e4b\u7c7b\u7684\u65b9\u6cd5\uff0c\u662f\u4e0d\u9700\u8981\u643a\u5e26csrftoken\u7684\uff0c\u518d\u6b21\u4f18\u5316\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    function csrfSafeMethod(method){\n        return (\/^(GET|HEAD|OPTIONS|TRACE)$\/.test(method));\n    }\n    \n    function bindSendSmsEvent(){\n        $.ajaxSetup({\n            beforeSend:function (xhr,settings){\n                if (!csrfSafeMethod(settings.type)){\n                    xhr.setRequestHeaders('X-CSRFTOKEN',getCookie('csrftoken'));\n                }\n            }\n        })\n    }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u5916\uff0c\u5176\u4ed6\u7684html\u9875\u9762\u53ef\u80fd\u4e5f\u9700\u8981\uff0c\u6545\u800c\u53ef\u4ee5\u8ba9\u5b83\u5355\u72ec\u505a\u4e2a\u6a21\u5757\u5f15\u5165\u5373\u53ef\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1126\" height=\"1257\" src=\"http:\/\/www.chan.ink\/wp-content\/uploads\/2024\/12\/1735368918788.png\" alt=\"\" class=\"wp-image-1181\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u6839\u636ecookie\u7684name\u83b7\u53d6\u5bf9\u5e94\u7684\u503c\n * @param name\n * @returns {null}\n *\/\nfunction getCookie(name) {\n    let cookieValue = null;\n    if (document.cookie &amp;&amp; document.cookie !== '') {\n        const cookies = document.cookie.split(';');\n        for (let i = 0; i &lt; cookies.length; i++) {\n            const cookie = cookies&#91;i].trim();\n            \/\/ Does this cookie string begin with the name we want?\n            if (cookie.substring(0, name.length + 1) === (name + '=')) {\n                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));\n                break;\n            }\n        }\n    }\n    return cookieValue;\n}\n\nfunction csrfSafeMethod(method) {\n    \/\/ these HTTP methods do not require CSRF protection\n    return (\/^(GET|HEAD|OPTIONS|TRACE)$\/.test(method));\n}\n\n$.ajaxSetup({\n    beforeSend: function (xhr, settings) {\n        if (!csrfSafeMethod(settings.type)) {\n            xhr.setRequestHeader(\"X-CSRFTOKEN\", getCookie('csrftoken'));\n        }\n    }\n})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8be6\u89c1Django\u6e90\u7801\u5206\u6790\u73ed7.09\u7ea625\u5206\u949f\u524d\u540e\u5904 &#8212;&#8212;&#8212;&#8212;&#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1170","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/1170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/comments?post=1170"}],"version-history":[{"count":2,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/1170\/revisions"}],"predecessor-version":[{"id":1182,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/posts\/1170\/revisions\/1182"}],"wp:attachment":[{"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/media?parent=1170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/categories?post=1170"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.chan.ink\/index.php\/wp-json\/wp\/v2\/tags?post=1170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}