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