0

Hi I am using django framework. The ajax response is not working. I have tried the code as follows. views.py

def ForgotUsername(request):
    if request.method=="POST":
        email=request.POST['email']
        usr=User.objects.all()
        question=""
        for i in usr:
            if email in i.email:
                id_email=i.id
                que=securityquestions.objects.get(user_id=i.id)
                question+=que.question   
        q={'question':question}
        return HttpResponse(json.dumps(q),mimetype='application/json')

    else:
        return render(request,'registration/ForgotPassword.html')

Html:

$("#email1").change(function()
            {
                var emailstring = $("#email1").val();
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if (!emailReg.test(emailstring))
                    {
                        $("#emailalert").html('<div class="alert alert-error"><button class="close" data-dismiss="alert" type="button">×</button>Not a valid Email</div>');
                        return false;
                    }
                else
                    {
                        $.ajax({
                            type:'POST',data:{email:emailstring},
                            url:'/registration/ForgotUsername/',
                            datatype:'json',
                            success:function(data) {
                                alert("email exists");
                                alert(data);
                                $('#ques').val(data);
                            }
                        });
                        return true;
                    }

            });

In this id "email1" is the email input field name. My ajax response is print the output in a aseparate page. It's not returning response to ajax. Help me to proceed

10
  • 2
    you should pass csrf_token . https://docs.djangoproject.com/en/dev/ref/contrib/csrf/ Commented Jan 29, 2014 at 6:54
  • In my form I added {% csrf_token %} Commented Jan 29, 2014 at 6:57
  • but you don't pass it. Commented Jan 29, 2014 at 6:58
  • could you give suggesstion to how to pass it Commented Jan 29, 2014 at 7:00
  • Can you paste the response you are getting on your Ajax call? Commented Jan 29, 2014 at 7:29

2 Answers 2

1

PASS csrf_token .

Your AJAX response should be

var CSRF_TOKEN = document.getElementsByName('csrfmiddlewaretoken')[0].value
$.ajax({
          type:'POST',data:{email:emailstring, csrfmiddlewaretoken: CSRF_TOKEN },
          url:'/registration/ForgotUsername/',
          datatype:'json',
          success:function(data) {
              alert("email exists");
              alert(data);
              $('#ques').val(data);
       }
Sign up to request clarification or add additional context in comments.

5 Comments

Nice answer :), easy and practical.
yes i have added, but still I didn't get. Actually I have written it in on change event. In my form i have input email field, next to that security question get field to recover my password.
While am giving tab email validation error i shown.. but not the question is getting displayed in the security question lable field
set DEBUG to True in your settings . so you will get exact error .
U can use from django.template.loader import render_to_string https://docs.djangoproject.com/en/dev/ref/templates/api/#the-render-to-string-shortcut . put print statements in views to debug it .
0

To achieve that in fast way, not the best you can add {% csrf_token %} into your form and pass the whole form in your ajax request by serializing it:

var yourForm = $("#your-form-id");

$.ajax({
    type:'POST',data:yourForm.serialize(),
    url:'/registration/ForgotUsername/',
    datatype:'json',
    success:function(data) {
        alert("email exists");
        alert(data);
        $('#ques').val(data);
    }
});

Serialization will convert the all form elements including csrf_token into desired data format.

2 Comments

yes i have added, but still I didn't get. Actually I have written it in on change event. In my form i have input email field, next to that security question get field to recover my password
While am giving tab email validation error i shown.. but not the question is getting displayed in the security question lable field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.