APIS commonly used in JQuery Ajax operations: Differences and usage of serializeArray, serialize, And param, jqueryui

Source: Internet
Author: User

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 ,"")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.