How can we post a complete HTML form in JSON format?

Solution

Often we have to send complete form data using Ajax to server. If the form data comprises of one or two parameter it can be achieved by manually forming the JSON data as shown below:

var data = {
              "name": $("#name").val(),
              "company" : $("#company").val()
           };
$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

But assume if you have huge amount of form data that you have to send then it is advised to use serialize the form using serializeArray() method as shown below

var data = JSON.stringify($("#myForm").serializeArray());
$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

Share this thread

Comments

Comments
comments powered by Disqus

Navigation

Social Media