Madanika
Madanika

Reputation: 119

Not returning ajax response?

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

Upvotes: 0

Views: 93

Answers (2)

Priyank Patel
Priyank Patel

Reputation: 3535

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);
       }

Upvotes: 1

scriptmonster
scriptmonster

Reputation: 2771

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.

Upvotes: 0

Related Questions