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);
}
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.
Source:stackexchange.com