Asynchronous submission of form data has two pain points:
1. garbled Coding
In fact, the bottom layer of ajax technology is unicode encoding. garbled code occurs when GBK pages interact with interfaces. There are multiple solutions, which can convert the page to UTF-8 encoding, or encodeURI encoding twice before sending the data, decoding once when receiving the data.
2. Get form field data
Taking YUI3 as an example, the native io-form module has been well encapsulated. There is a private method named _ serialize, which can pre-encode the asynchronous form submission behavior:
Copy to ClipboardReference: [www.bkjia.com] data: encodeURI (Y. io. _ serialize ({
Id: '# formId ',
UseDisabled: true
}))
That is, a private method of zookeeper is used, which looks perfect. If the name attribute of a field does not contain special symbols such as [], it is normal.
The principle of this _ serialize method is very simple. You can retrieve input, select, textarea, and other fields in the node, and then traverse and set the value according to the name.
A simple code case (YUI3 ):
Copy to ClipboardReference: [www.bkjia.com] var parameters = [], nodeList;
NodeList. each (function (v ){
Parameters. push (v. get ('name') + '=' + Y. Lang. trim (v. get ('value ')));
});
Parameters = parameters. join ('&');