JavaScript form submission
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Development tools and key technology: Visual Studio 2015 and JavaScript
Author: Huang Futao
Writing time: on April 30, 2020,
Knowledge points list
1, Form Form attribute
(1) the action: the equivalent of a path (URL), provisions when submitting the form to the server sends the form data
URL possible values:
Absolute URL - pointing to other sites (such as the SRC="https://bbs.csdn.net/topics/www.baidu.com"),
Relative URL - pointing at the site files (such as SRC="https://bbs.csdn.net/Form/getData") description: [the first value as the controller name, the second value is the name of the controller methods],
(2) method: get/post two values, regulations for sending form - the data of the HTTP method, (submission form)
For example: & lt; The form action="/form/getData method=" post "& gt;
In detail the get and post
Get submitted directly exposed to the URL parameters,
(W3School given on difference:)
Compared with the POST, GET more simple and faster, and in most cases are available,
However, in the following cases, please use the POST request:
1. Unable to use the cache file (update the file on the server or database)
2. Huge amounts of data can be sent to the server (POST there is no limit to the amount of data)
3. Send contains unknown character of user input, POST than GET more stable and more reliable
Get/post difference:
1. The GET method is used to GET the data to the server, and PSOT is used to transfer modify data on the server,
2. GET the data in a form is added to the action points to the URL, and between the use of "?" Connection, and between each variable to use "& amp;" The connection; PSOT is the form of the data in the form of data (FormData) in the body, according to the keys on the way, is passed to the points of action
3. It is not safe to GET, because in the process of transmission, data is placed on the requested url so that users can directly on the browser to see the data submitted, the POST all the operations are not visible to the user, the data in data volume (FormData)
4. The GET method to the URL to add data, the length of the URL is restricted maximum length is 2048 characters (URL), POST submission unlimited
5. GET to the default submission form form
6. The GET method to GET the data after the refresh will not have a negative impact, because it is just to GET the data,
POST data will be resubmitted may have adverse consequences (browser should inform the user data will be resubmitted)
7. The data type of restrictions: GET allow only ASCII characters, the POST is unrestricted (if submitted binary data (such as picture), you need to use the POST method)
Note: be sure to call to form method form. Submit ();
2, the form to get the data of four methods
The first method:
- to receive data via a parameter (the get and post way can receive)
- use ", "comma between parameter
- define parameters must be consistent and attribute in the form, otherwise unable to receive data
Public ActionResult GetData (string name, string sex, string address)
{
String STR="1. Receive the form submission data by means of parameter" + name + "& amp;" + sex + & amp; "" + address;
Return the Content (STR);
}
The second method:
- through FormCollection to receive the form data (only receiving data are submitted to the post of)
Public ActionResult getDataByFormCollection (FormCollection form)
{
String name=form (" name "),
String sex=form (" sex ");
String address=form (" address ");
String STR="2. To receive the form data by FormCollection" + name + "& amp;" + sex + & amp; "" + address;
Return the Content (STR);
}
The third method:
- by Request. The Form/" name attribute value "to obtain the Form data (only receiving data are submitted to the post of)
Public ActionResult getDataByRequest ()
{
String name=Request. The Form (" name "),
. String sex=Request Form (" sex ");
. String address=Request Form (" address ");
String STR="3. Through the Request. The Form to receive the data Form" + name + "& amp;" + sex + & amp; "" + address;
Return the Content (STR);
}
The fourth method:
- by creating EntityClass entity class receive data (the get and post way can receive)
Public ActionResult getDataByEntityClass (Person to Person)
{
String name=person. Name;
String sex=person. Sex;
String address=person. The address;
String STR="4. EntityClass entity class receive data" + name + "& amp;" + sex + & amp; "" + address;
Return the Content (STR);
}
//js in Person through the constructor to create the object
//create the entity class
Public class Person {
Public string name {get; set; }
Public string sex {get; set; }
Public string address {get; set; }
}