While trying to setup password reset process using Dj-rest-auth package. I ran into an issue getting the submit button to do the post request to rest_password_reset_confirm api endpoint. I keeping getting error message relating to 'ContentType' and $.post method only performing GET request. I took the form and everything from the demo app in dj-rest-auth here.
I had to re-write the ajax script in base.html to below for it to work. I also added two new div for success and failure response.
<div class="form-group api-response-success" style="color: green;"></div>
<div class="form-group api-response-error" style="color: red;"></div>
$().ready(function(){
$('form.ajax-post button[type=submit]').click(function(e){
var form = $('form.ajax-post');
var url = form.attr('action');
data = form.serializeArray();
var object_with_key_value = {};
$.map(data, function(n, i){
object_with_key_value[n['name']] = n['value'];
});
$.ajax({
'url': url,
'type':"POST",
'data': JSON.stringify(object_with_key_value),
'contentType': 'application/json',
'success': function(data){
$('.api-response-success').html("Password has been reset with the new password.");
},
'error': function(data){
$('.api-response-error').html("Something went wrong, please try again.");
},
});
e.preventDefault();
})
});