Monday, March 27, 2017

Re: Django Captcha Ajax call not working

Hi,

First of all - you are not sending anything to the form. 

            $.ajax({
                type: "POST",  
                url: "../captcha",
                contentType: "application/json",
                data: {},
                dataType: "json",
                success: function(data) {
                    alert("success");
                  },
                error: function(data) {
                    alert("not OK");
                  }

                });

This says that the data sent to the method SHOULD be empty - so the form will always be invalid - there is nothing in it. You probably want to include your form in the data part of the jquery request. Check this for more information: https://api.jquery.com/serialize/.

Secondly - you should also include the CSRF token - otherwise you will get other errors with that eventually - check this for information on how to do that: https://docs.djangoproject.com/en/1.10/ref/csrf/#ajax

Regards,

Andréas

2017-03-27 10:22 GMT+02:00 valerio orfano <ingorfano2@gmail.com>:
Hi, I managed to make an ajax call. But the form is always invalid. IT is like the captcha field passed to the view is always empty.

valerio

On Monday, March 27, 2017 at 8:48:17 AM UTC+2, valerio orfano wrote:
I have the following class view taken from documentation:

class CaptchaView(CreateView):
    template_name = "captcha.html"
    form_class = MyForm
   
    def form_invalid(self, form):
        if self.request.is_ajax():
            to_json_response = dict()
            to_json_response['status'] = 0
            to_json_response['form_errors'] = form.errors

            to_json_response['new_cptch_key'] = CaptchaStore.generate_key()
            to_json_response['new_cptch_image'] = captcha_image_url(to_json_response['new_cptch_key'])

            return HttpResponse(json.dumps(to_json_response), content_type='application/json')
        else:
            return HttpResponse("test1", content_type='application/json')

    def form_valid(self, form):
        if self.request.is_ajax():
            to_json_response = dict()
            to_json_response['status'] = 1

            to_json_response['new_cptch_key'] = CaptchaStore.generate_key()
            to_json_response['new_cptch_image'] = captcha_image_url(to_json_response['new_cptch_key'])

            return HttpResponse(json.dumps(to_json_response), content_type='application/json')
        else:
            return HttpResponse("test2", content_type='application/json')


and the following ajax call from template:

    <script type="text/javascript">
        function captcha() {
            $.ajax({
                type: "POST", 
                url: "../captcha",
                contentType: "application/json",
                data: {},
                dataType: "json",
                success: function(data) {
                    alert("success");
                  },
                error: function(data) {
                    alert("not OK");
                  }

                });
        }
    </script>


<input type="button" id="button" onclick="captcha()" value="OK"/>

url(r'^captcha$', views.CaptchaView.as_view(), name="captcha")

Could you please help me to get a proper ajax call? When i click the OK button no success or error message is displayed!!!

Please help me out!!

thanx

valerio


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/32c46521-92be-40be-820a-fb5fe63c4620%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK4qSCdhmMa7Zxnh-e3Q7ccA-h9b%2Bn0XSOVrGttVVVQWQ79oAA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment