The difference and use of Api:serializearray, serialize, Param used in JQuery AJAX operations

Source: Internet
Author: User

When using jquery for Ajax coding, these 3 APIs are often used, and this article learns how to use the 3 APIs. such as the next HTML fragment, if we submit the control value under 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"). Serialize () The output is a=1&b=2&c=3&f=8&f=9

$ ("#holder"). The Serializearray () output results are as follows:

[   {name: ' A ', Value: ' 1 '},   {name: ' B ', Value: ' 2 '},  {name: ' C ', Value: ' 3 '},  {name: ' F ', Value: ' 8 '},  {name: ' F ', Value: ' 9 '}]

As you can see: bothserialize and Serializearray operate on the jquery object (the selected form element), but the return value format is different.

It is important to note here: These 2 APIs can only manipulate the form, and if you change the holder to a 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 = Decodeur IComponent ($.param (obj)); alert (recursiveencoded); alert (recursivedecoded);

The output 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

You can see that when you use Param to convert JSON to HTML parameters, special characters are encoded. For JavaScript coding, refer to this article.

The return of the recursivedecoded shows that the serialization of the Param array is not the result we want. You can use regular expression conversions:

Recursivedecoded.replace (/\[\]/g, "")

JQuery Ajax Operations Common Api:serializearray, serialize, param differences and use

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.