Ajax Data parameters

Source: Internet
Author: User

The form uses Serializearray to get all:

<formID= ' AddForm 'Action= ' useradd.action 'type= ' Post '>     <label for= ' uname '>User name</label>:<inputtype= ' text 'name= ' uname 'ID= ' uname '><BR>     <label for= ' Mobileipt '>Phone Number:</label><inputtype= ' text 'name= ' Mobileipt 'ID= ' Mobileipt '><BR>     <label for= ' Birthday '>Birthday:</label><inputtype= ' text 'name= ' Birthday '><BR>     <inputtype= ' button 'value= ' Submit 'onclick= ' addUser () '></form>

$ (' #addForm '). Serializearray (); // returns the data structure, which is a JSON array, with each pair of images name and value key, representing the name and value of this form element [    {"name": "Uname", "Value": "},    {" name ":" Mobileipt "," Value ":" "},       { "name": "Birthday", "Value": ""}]

We use the Jquery.param () method to process:

var arr = $ (' #addForm '). Serializearray (); $.param (arr);" Uname=alice&mobileipt=110&birthday=1983-05-12 "

Here we can just fill in the JSON array directly in the data of Ajax, and call $.param () processing within jquery itself.

Let's take a look at the description of the Jquery.param () method:

return value: Stringjquery.param (Obj,[traditional]) serializes the number of table cells or objects. The parameter obj,[traditional] array or jquery objectis serialized according to the name/value, and the normal object is serialized according to the key/value pair. Traditional: Whether to use the traditional method of shallow serialization. Demo:$.param ({uanme:' Vic ', Mobileipt: ' A ', ' Birthday: ' 2013-11-11 '});" Uanme=vic&mobileipt=110&birthday=2013-11-11 "

Look at the explanation, seems to have nothing to do with us ah, we change a JSON array to see

$.param ([{uanme: ' Vic '},{mobileipt: '},{birthday: ' 2013-11-11 '}]);" Undefined=&undefined=&undefined= "

The conversion is unsuccessful, why is the data of our form successfully converted to URL parameters? Let's look at the jquery source.

//In the Ajax () method, the JSON-type data is $.param () processedif(S.data && S.processdata &&typeofS.data!== "string") {S.data=Jquery.param (S.data, s.traditional);}//in the Param methodif(Jquery.isarray (a) | | (A.jquery &&!)Jquery.isplainobject (a))) {        //Serialize the form elementsJquery.each (A,function() {Add ( This. Name, This. Value);    }); } Else {        //If Traditional, encode the "old" (the 1.3.2 or older        //Did it), otherwise encode params recursively.         for(Prefixincha) {buildparams (prefix, a[prefix], traditional, add); }    }

Now that's clear, if it's JSON data, loop through them, just take their name property and the Value property to stitch the string.

If it is a normal object, iterate over the object's properties and then stitch the string.

Summarize:

So what this article is going to say is that in the Ajax function of jquery, you can pass in 3 types of data

1. Text: "Uname=alice&mobileipt=110&birthday=1983-05-12"

2.json object: {uanme: ' Vic ', Mobileipt: ' A ', Birthday: ' 2013-11-11 '}

3.json Arrays:

[    {"name": "Uname", "Value": "Alice"},    {"name": "Mobileipt", "value": "{"},       {"name": "Birthday", "value" : "2012-11-11"}]
So, we can get the form and submit it in one click, it's very convenient.

Add:
In fact, the extraction of form data only requires the Serialize () method to obtain the "uname=alice&mobileipt=110&birthday=1983-05-12" directly.

Ajax Data parameters

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.