Ajax Call is sending empty request.POST or request.body

472 views
Skip to first unread message

valerio orfano

unread,
Mar 30, 2017, 9:52:53 AM3/30/17
to Django users
Hi i am still not clear when using request.POST or request.body.

Anyway this ajax call is sending emprty request.POST, or empty  request.body:

    <script type="text/javascript">
    $(document).ready(function(){

        $(".captcha_form").submit(function(event){
            var check = $('#id_check').val();
            alert(check);
            $.ajax({
                type: "POST", 
                url: "./captcha",
                contentType: "application/json",
                data:check,
                dataType: "json",
                success: function(data) {
                    alert("passa");
                    alert(data.status);
                   
                  },
                error: function(data) {
                    alert("NOT_OK");
                  }         
            });
       
    });
    });
   
    </script>


<form id ="captcha_form" class="captcha_form" action="./captcha" method="post">{% csrf_token %}
    {{form}}
<input type="submit" id="button" class="button" value="Invio"/>
</form>


class check_box_form(forms.Form):
    scelte = [('Yes','Yes'),('No','No')]
    check = forms.ChoiceField(label='I am a Robot       ',choices=scelte)

Camilo Torres

unread,
Mar 31, 2017, 7:37:16 AM3/31/17
to Django users
Hi.

The difference is explained here:
https://docs.djangoproject.com/en/1.10/ref/request-response/

You are sending the data in the ajax call as Jason (I suppose, from the content type), then in your view you should probably use request.body to read the json document.

I also noticed you are using a django form. In that case you can change your ajax call to send a form encoding content type and serialize the html form as the data. In your view you can then use your django form to process the data from request.POST.

Reply all
Reply to author
Forward
0 new messages