0
template.html
<script >
$(document).ready(function() {          


var a="{{parameter}}";

    $.ajax({
    type :'GET',

    url : geturl(a),

    dataType : 'json',


views.py
if param=="daywise":

    print request.method
    if request.method=="POST":


        if request.POST.get('monyrsubmit'):
            monthform=MonthForm(request.POST)
            if monthform.is_valid():
                selected_month=monthform.cleaned_data["Month"]
                selected_year=monthform.cleaned_data["Year"]
                print selected_month
                print selected_year

我实际上是通过发送表单数据来执行 post 方法的。但它有一个获取请求,因为我已经在 ajax 脚本中给出了 GET 类型。我正在查看视图。如果 request.method=="POST" 但此方法仍然是 GET

4

1 回答 1

0

将类型更改为 POST...

var a="{{parameter}}";

    $.ajax({
    type :'POST',

    url : geturl(a),

    dataType : 'json',

如果您没有在 django 中禁用 CSRF 中间件,则会导致问题。<script>要修复它,请在标记之后和之前添加此脚本$(document).read...

$.ajaxSetup({ 
        beforeSend: function(xhr, settings) {
            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 = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                     }
                 }
             }
             return cookieValue;
         }
         if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
             // Only send the token to relative URLs i.e. locally.
             xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
         }
     } 
 });
于 2013-02-19T08:23:00.497 回答