A brief talk on Get and post in Ajax

Source: Internet
Author: User

Get mode:
Simple data can be transferred in a Get mode, but the size is generally limited to 1KB, and the data is appended to the URL (HTTP header delivery), that is, the browser appends the individual form field elements and their data to the resource path in the request line as a URL parameter. The most important point is that it will be cached by the client's browser, so that others can read the customer's data from the browser's history, such as the account number and password. Therefore, in some cases, the Get method poses a serious security problem.

Post mode:
When a post is used, the browser sends each form field element and its data to the Web server as the entity content of the HTTP message, rather than as a parameter to the URL address, and the amount of data that is passed by post is much larger than the amount of data that is sent using the Get method.

In short, the Get method transmits small amounts of data, high processing efficiency, low security, will be cached, and post instead.

Use the Get method to be aware of:
1 for a GET request (or any that involves a URL pass parameter), the passed arguments are processed first by the encodeURIComponent method. Example: var url = "Update.php?username=" +encodeuricomponent ( username) + "&content=" +encodeuricomponent

(content) + "&id=1";


Use the Post method to be aware of:
1. Set the header's Context-type to Application/x-www-form-urlencode to ensure that the server knows that there are parameter variables in the entity. Typically use the XMLHttpRequest object's setRequestHeader ( "Context-type", "application/x-www-form-urlencoded;"). Cases:

Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
2. The parameter is the key value pair corresponding to the name/value one by one, separated by the & number for each pair. As Var name=abc&sex=man&age=18, pay attention to Var name=update.php?

Abc&sex=man&age=18 and the name=?abc&sex=man&age=18 of Var are all wrong;
3. The parameters are sent in the Send (parameter) method, for example: Xmlhttp.send (name); If it is get mode, direct xmlhttp.send (NULL);

4. The server-side request parameter distinguishes between get and post. If it is GET mode then $username = $_get["username"]; If it is POST mode, then $username = $_post["username"];

The post and get methods have the following differences:
1.Post when transferring data, you do not need to display it in the URL, and the Get method is displayed in the URL.
The 2.Post transmits a large amount of data, which can reach 2 m, while the Get method can only pass approximately 1024 bytes due to URL length limitations.
3.Post as the name implies, is to transfer data to the server segment, get is to get data from the server segment. And get can also transmit data, just to design tell the server, you need what kind of data. The post information is the content of the HTTP request, and get is transmitted over the HTTP header.

Get method is received with request.querystring["StrName"]
The Post method is received with request.form["StrName"]

Attention:
Although the two submissions can be unified with request ("StrName") to obtain the submission data, but this has an impact on the efficiency of the program, is not recommended to use.
In general, try to avoid submitting forms using get, as they can cause security issues

The template to which the actual project was applied, and the following are the get and POST

Get template Grooming in Ajax

Appcan.ajax ({
Url:baseurl + ' sumec/applyid?applyuser= ' + localstorage.getitem (' user_id ') + ' &moduleid= ' + localStorage.getItem (' ModuleID '),
Url:baseurl + ' sumec/applyid?applyuser= ' + user_id + ' &moduleid=1 ',
Type: ' GET ',
Data: ",
ContentType: ' Application/json ',
DataType: ' JSON ',
timeout:5000,
Success:function (data) {
if (data.status! = ' 0 ') {
Appcan.window.openToast (' failed to apply for serial number ', 2000);
Console.error (' res error ');
} else {
Applyid = Data.applyid;
Console.log (Applyid);
}
}
});

Note:

1,url need Url:baseurl + ' sumec/applyid?applyuser= ' + user_id + ' &moduleid=1 ',

Post template grooming in Ajax

Appcan.request.ajax ({
Type: ' POST ',
Url:baseurl + ' Sumec/flow ',
Data:formdat,
ContentType: ' Application/json ',
DataType: ' JSON ',
Success:function (data) {
Console.log (data);
if (data.status! = ' 0 ') {
Appcan.window.openToast (' The process failed to submit ', 2000);
Console.error (' res error ');
} else {

}
}
});

Note:

1,url processing BaseUrl + ' sumec/flow ', just write a good path

A brief talk on Get and post in Ajax

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.