-2

I have a form on a webpage and I also have some JSON data in Javascript, how do I use JQuery to append the JSON data on form post? Here are some example:

<form action="server.cshtml">

</form>

<script>
    var data = {
        data1: "aaa",
        data2: "bbb"
    };

    $("form").submit();
</script>
Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174
  • 1
    I think this could be useful http://stackoverflow.com/questions/12055435/append-json-data-to-post-request – sebasaenz Dec 14 '16 at 23:38

1 Answers1

0

first serialize the form into an array.

then add the new data to that array.

then post to your location.

$(document).ready(function() {
    $('form').on('submit', function(){
        var formData = $('form').serializeArray();
        formData.push({data1: 'aaa' })
        $.post('somehwere', formData);
    });
});
BenBlanch
  • 11
  • 4