n4mi n4mi - 2 months ago 14
jQuery Question

jquery passing serialize form in $.post()

I have this function and it executes upon submitting a form. How can i serialize the whole form and pass it?

function addToCart(el)
{
console.log(el.action);



$.post(el.action, { id: 'df'},
function(returnedData){
console.log(returnedData);
});

}

Answer

You can do something like this:

  var api = 'http://localhost/api';
  $( "form" ).on( "submit", function(event) {
    event.preventDefault();
    var payload = $( this ).serializeArray();

    $.post(api, payload, function(response) {
      console.log("response", response);
    });
  });