{"id":2069,"date":"2023-04-01T21:39:58","date_gmt":"2023-04-01T13:39:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2069"},"modified":"2023-04-06T16:01:51","modified_gmt":"2023-04-06T08:01:51","slug":"django-post-request-error-csrf-token-missing-or-incorrect-resolution","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/django-post-request-error-csrf-token-missing-or-incorrect-resolution\/","title":{"rendered":"Django POST\u8bf7\u6c42\u62a5\u9519CSRF token missing or incorrect\u89e3\u51b3"},"content":{"rendered":"<p>\u5728JS\u4e2d\uff0c\u4f7f\u7528post\u65b9\u6cd5\u63d0\u4ea4\u6570\u636e\u5230Django\u540e\u53f0\uff0c\u5982\u679c\u9875\u9762\u6ca1\u6709\u505a\u8de8\u7ad9\u4f2a\u9020\uff0c\u5219\u4f1a\u88ab\u6d4f\u89c8\u5668\u62d2\u7edd\u8bbf\u95ee\uff0c\u62a5\u9519\u5982\u4e0b\uff1a<\/p>\n<pre><code>Forbidden (CSRF token missing or incorrect.): \/appblog\/payment\n[18\/Jun\/2020 08:52:08] &quot;POST \/appblog\/payment HTTP\/1.1&quot; 403 2513<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u89e3\u51b3\u65b9\u6cd5\uff1a\u524d\u7aefajax\u8bf7\u6c42\u63d0\u4ea4<code>CSRF token<\/code>\u4fe1\u606f<\/p>\n<pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;zh&quot;&gt;\n{% csrf_token %}\n&lt;head&gt;\n\n...\n\n&lt;script&gt;\n    ...\n    var csrfToken = $(&quot;[name=&#039;csrfmiddlewaretoken&#039;]&quot;).val();\n    \/\/\u65b9\u6cd5\u4e00\n    $.ajaxSetup({\n        data: {csrfmiddlewaretoken: csrfToken}\n    })\n    $.ajax({\n        url: &#039;\/payment&#039;,\n        type: &#039;post&#039;,\n        contentType: &#039;application\/json;charset=utf-8&#039;,\n        data: data,\n        async: true,\n        \/\/\u65b9\u6cd5\u4e8c\n        headers: {&#039;X-CSRFToken&#039;: csrfToken},\n        \/\/\u65b9\u6cd5\u4e09\n        beforeSend: function(xhr, e) {\n            xhr.setRequestHeader(&#039;X-CSRFToken&#039;, csrfToken)\n        },<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728JS\u4e2d\uff0c\u4f7f\u7528post\u65b9\u6cd5\u63d0\u4ea4\u6570\u636e\u5230Django\u540e\u53f0\uff0c\u5982\u679c\u9875\u9762\u6ca1\u6709\u505a\u8de8\u7ad9\u4f2a\u9020\uff0c\u5219\u4f1a\u88ab\u6d4f\u89c8\u5668\u62d2\u7edd\u8bbf\u95ee\uff0c\u62a5\u9519\u5982\u4e0b\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[169,522],"class_list":["post-2069","post","type-post","status-publish","format-standard","hentry","category-python","tag-csrf","tag-django"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2069"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2069\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}