0

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.

Community
  • 1
  • 1
ian
  • 12,003
  • 9
  • 51
  • 107

1 Answers1

0

This is the jquery source from (http://jquery.com/) version 1.8.2 for building the params prior to a ajax call


function buildParams( prefix, obj, traditional, add ) {
    var name;

    if ( jQuery.isArray( obj ) ) {
        // Serialize array item.
        jQuery.each( obj, function( i, v ) {
            if ( traditional || rbracket.test( prefix ) ) {
                // Treat each array item as a scalar.
                add( prefix, v );

            } else {
                // If array item is non-scalar (array or object), encode its
                // numeric index to resolve deserialization ambiguity issues.
                // Note that rack (as of 1.0.0) can't currently deserialize
                // nested arrays properly, and attempting to do so may cause
                // a server error. Possible fixes are to modify rack's
                // deserialization algorithm or to provide an option or flag
                // to force array serialization to be shallow.
                buildParams( prefix + "[" + ( typeof v === "object" ? i : "" ) + "]", v, traditional, add );
            }
        });

    } else if ( !traditional && jQuery.type( obj ) === "object" ) {
        // Serialize object item.
        for ( name in obj ) {
            buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
        }

    } else {
        // Serialize scalar item.
        add( prefix, obj );
    }
}

From the code listed, you see that they all the 'ways' you listed above are all handled within the handling of the params within the above listed function and end up in the same stringified version

ilan berci
  • 3,883
  • 1
  • 16
  • 21
  • The quotes are to signify that we are going to be invaded by space aliens in December 2012 and you should take precautions as soon as possible – ilan berci Sep 28 '12 at 17:29