Hey there, I'm a beginner in javascript and I'm having some trouble submitting a form using pure javascript.
Here is my code:
var myform = document.getElementById('js-post-form');
myform.addEventListener('submit', function(e){
e.preventDefault();
var request = new XMLHttpRequest();
request.open(myform.method, myform.action, true);
request.setRequestHeader('X-CSRFToken', cookies['csrftoken']);
request.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
request.onload = function () {
if (this.status >= 200 && this.status < 400) {
console.log("ready");
var data = JSON.parse(this.response);
console.log('success');
} else {
console.log("not ready yet");
};
};
request.onerror = function() {
console.log("connection error");
};
request.send();
});
});
Can anyone help me figure out what I'm missing?
Thanks in advance!