Note: I've seen a lot of questions about this, but each gives a slightly different answer, I'd like to know what is canonical or best practice because of the differing answers. None of them is clear as to why there is a difference, or when to use one or the other. Some are just out of date.
Jquery version 1.8.0, the examples use coffeescript.
I have a form with data, and on clicking submit the way to get the data and post it is one of three things according to what I've read: (if there are undeclared vars in the examples below, please assume they've been assigned elsewhere)
1:
data = $.param(form.serializeArray())
$.ajax( url, {
headers: {
Accept : "application/json",
"Content-Type": "application/json"
},
dataType: "json",
type: "POST",
data: data,
posting jquery .serializeArray(); output through ajax
2. Same as (1) except for this line:
data = JSON.stringify(form.serializeArray())
Send post form data in json format via ajax with JQuery dynamically
3. Same as (1) except for this line:
data = form.serialize()
http://api.jquery.com/jQuery.post/#example-3
This may explain why it's better to use $.param
, but it's an old post about jQuery 1.4.