这是设计使然。您的POST数据必须包含
csrfmiddlewaretoken值。您可以从cookie中获取它,然后将其与POST请求一起发送。详细信息在这里。对于您的特定情况,您可以执行以下操作-
<script>$(function () { function getcookie(name) { var cookievalue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookievalue = depreURIComponent(cookie.substring(name.length + 1)); break; } } } return cookievalue; } var csrftoken = getcookie('csrftoken'); $("button") .button() .click(function (event) { var postdata = { 'value1': 7, 'value2': 5, 'csrfmiddlewaretoken': csrftoken }; $.post('', postdata); // POST request to the same view I am now window.alert("Hello world!"); // To know it is working });});</script>


