APIS commonly used in JQuery Ajax operations: Differences and usage of serializeArray, serialize, And param, jqueryui
When using JQuery for ajax encoding, these three APIs are often used. This article describes how to use these three APIs. The following HTML snippet submits the control values in the holder form to the server. You need to use serialize or serializeArray.
<form id="holder"> <input type="text" name="a" value="1"/> <div><input type="text" name="b" value="2" id="b" /> </div> <input type="hidden" name="c" value="3" id="c" /> <div> <input type="checkbox" name="f" value="8" checked="true"/> <input type="checkbox" name="f" value="9" checked="true"/> </div></form>
$ ("# Holder"). The output result of serialize () is a = 1 & B = 2 & c = 3 & f = 8 & f = 9
$ ("# Holder"). serializeArray () the output result is as follows:
[ {name: 'a', value: '1'}, {name: 'b', value: '2'}, {name: 'c', value: '3'}, {name: 'f', value: '8'}, {name: 'f', value: '9'}]
We can see that both serialize and serializeArray operate on JQuery objects (the selected FORM element), but the return value format is different.
Note:These two APIs can only operate formIf you change holder to div, you will find that it does not work.
var obj = {"a":{one: 1,two: 2,three: 3}, "b": [1,2,3]};var recursiveEncoded = $.param(obj);var recursiveDecoded = decodeURIComponent($.param(obj));alert(recursiveEncoded);alert(recursiveDecoded);
The output result is:
a%5Bone%5D=1&a%5Btwo%5D=2&a%5Bthree%5D=3&b%5B%5D=1&b%5B%5D=2&b%5B%5D=3a[one]=1&a[two]=2&a[three]=3&b[]=1&b[]=2&b[]=3
We can see that when param is used to convert json into html parameters, special characters are encoded. For javascript encoding, refer to this article.
The returned results of recursiveDecoded show that param serialization of arrays is not what we want. You can use regular expression conversion:
RecursiveDecoded. replace (/\ [\]/g ,"")