There are two ways to send the form data through ajax:
-
making query string manually and passed it as ajax posted data. for e.g.
$.ajax(function(){ type: 'post', data:"val1="+$("#input1").val()+"&val2="+$("#input1").val(), ..... }); -
Use $(‘#myForm’).serialize() method for e.g.
$.ajax(function(){ type: 'post', data:$("#myForm").serialize(), ..... });
What is the key difference between sending the form data through these two ways.
The .serialize() method creates a text string in standard URL-encoded notation. It operates on a jQuery object representing a set of form elements. The form elements can be of several types:
The .serialize() method can act on a jQuery object that has selected individual form elements, such as , , and . However, it is typically easier to select the tag itself for serialization:
This produces a standard-looking query string: