jQuery.param( obj ) Returns: String
Description: Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.
-
version added: 1.2jQuery.param( obj )
-
objType: Array or PlainObjectAn array or object to serialize.
-
-
version added: 1.4jQuery.param( obj, traditional )
-
objType: Array or PlainObjectAn array or object to serialize.
-
traditionalType: BooleanA Boolean indicating whether to perform a traditional "shallow" serialization.
-
This function is used internally to convert form element values into a serialized string representation (See .serialize() for more information).
As of jQuery 1.3, the return value of a function is used instead of the function as a String.
As of jQuery 1.4, the $.param()
method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting jQuery.ajaxSettings.traditional = true;
.
If the object passed is in an Array, it must be an array of objects in the format returned by .serializeArray()
1 2 3 |
|
Note: Because some frameworks have limited ability to parse serialized arrays, developers should exercise caution when passing an
obj
argument that contains objects or arrays nested within another array.
Note: Because there is no universally agreed-upon specification for param strings, it is not possible to encode complex data structures using this method in a manner that works ideally across all languages supporting such input. Until such time that there is, the
$.param
method will remain in its current form.
In jQuery 1.4, HTML5 input elements are also serialized.
We can display a query string representation of an object and a URI-decoded version of the same as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
The values of recursiveEncoded
and recursiveDecoded
are alerted as follows:
a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3 a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3
To emulate the behavior of $.param()
prior to jQuery 1.4, we can set the traditional
argument to true
:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
The values of shallowEncoded
and shallowDecoded
are alerted as follows:
a=%5Bobject+Object%5D&b=1&b=2&b=3
a=[object+Object]&b=1&b=2&b=3
Examples:
Example: Serialize a key/value object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
|
Example: Serialize a few complex objects
1
2
3
4
5
6
7
8
9
10
|
|