I recently encountered a problem with an AJAX request that I haven't been able to resolve. Here's the code snippet in question:
<script type="text/javascript">
$('#activeform').on('submit', function(event){
event.preventDefault();
var _self = $(this);
var token = $('input[name="csrfmiddlewaretoken"]').val();
alert( _self.serialize()) // sanity check
$.ajax({
type: _self.attr('method'),
url: _self.attr('action'),
data: _self.serialize(),
contentType: "application/x-www-form-urlencoded;charset=utf-8",
headers: {"X-CSRFToken": token},
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept-Charset","utf-8");
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");},
success: function(json) {
alert("Send!")},
error: function( xhr, textStatus ) {
alert( [ xhr.status, textStatus ] )},
complete: function() {
alert("Complete")},
crossDomain: false
});
return
}); </script>
The AJAX request seems to work fine as I get success and complete messages, and my browser recognizes it as a POST request on submission. However, when checking for request.method == 'POST' in my Django view, it returns False. Any ideas on what might be causing this issue?