[Fixed]-Submitting all forms on a page with JavaScript using ajax at once to Django

1👍

Your collect function can be simplified and then you can post all the data as json either with serializeArray or serialize, for example:

var alldata = {};

$("form").each(function(i, form){
  var current_data = $(form).serializeArray();
  alldata[$(form).prop("id")]  = current_data;
});
alert(JSON.stringify(alldata));

$.ajax({
  //your parameters here: url, etc.
  url: 'save_stacks',
  type: 'post',
  dataType: 'json',
  data: alldata,
  success: function(response) {
    //handling code
  }
});

https://jsfiddle.net/1pLn4hw1/

You will likely have to adapt your handling code in the backend.

Leave a comment