django 开启了CSRF功能导致jquery ajax post数据报错

解决方法在post数据里引入csrfmiddlewaretoken: '{{ csrf_token }}'},同时需要在form表单中设置{% csrf_token %}


<script src="/static/jquery/dist/jquery.min.js"></script>
                  <script>
                       $(function(){
                            $("#postset").click(function() {
                                $.post("", {newpasswd1: "John", newpasswd2: "John",csrfmiddlewaretoken: '{{ csrf_token }}'},function (data) {
                                            alert("Data Loaded: " + data);
                                        });
                            });
                        });
                  </script>

 

 

 

相关文章:

  • 2021-12-10
  • 2021-10-25
  • 2021-10-17
  • 2021-08-24
  • 2021-09-28
猜你喜欢
  • 2021-08-04
  • 2022-01-19
  • 2021-09-09
  • 2021-11-05
  • 2021-05-30
  • 2022-12-23
  • 2021-11-04
相关资源
相似解决方案