0

Hello hello javascript newbie here.

I am trying to submit a form with pure javascript but failing :

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();
  });

});

What am I missing ?

thank you!

2 Answers 2

2

You are not sending anything to the server. Request body should be sent with send() method. Try this:

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(data); // returns the dictionnary { selected_elements:[] } 
        console.log('success');
      } else {
        console.log("not ready yet");
      };
    };
    request.onerror = function() {
      console.log("connection error");
    };
    request.send(myform); // Passed the form to be send with request body.
  });

});

Mozilla Docs on send() method.

Sign up to request clarification or add additional context in comments.

2 Comments

aaaah how I wish you were right ! So I added myform to request.send() and I still get an empty list... :(
did you get any solution?
1

Using FormData was the answer. Thank you Nalin for putting me on the right track !

the JS :

var myform = document.getElementById('js-post-form');
myform.addEventListener('submit', function(e){
    var formData = new FormData(myform);
    e.preventDefault();
    var request = new XMLHttpRequest();
    request.open(myform.method, myform.action, true);
    request.setRequestHeader('X-CSRFToken', cookies['csrftoken']);
    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(data); // returns the dictionnary { selected_elements:[] } 
        console.log('success');
      } else {
        console.log("not ready yet");
      };
    };
    request.onerror = function() {
      console.log("connection error");
    };
    request.send(formData);
  });

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.