Differences between get and post in Ajax

Source: Internet
Author: User

About Ajax get

And post

Difference

Get

Method:

Use get

Simple data can be transmitted, but the size is generally limited to 1 kb, and the data is appended to the URL for sending (HTTP header transfer), that is, the browser will

Each form field element and its data are appended to the resource path in the request line according to the URL parameter format. The most important thing is that it is

When the browser is cached, other users can read the customer's data, such as accounts and passwords, from the browser's historical records. Therefore, in some cases

Under, get

This method may cause serious security issues.

Instance:

// Get submit <br/> var postcontent = <br/> "name =" + encodeuricomponent ("Buford early ") + "& Email =" + encodeuricomponent ("buford@known-space.com"); <br/> // The parameter is appended to the URL <br/> var url = "www, Baidu.com "; <br/> XMLHTTP. open ("get", URL + "? "+ Postcontent, true); <br/> XMLHTTP. Send (null );

Post

Method:

When using post

The browser sends the form field elements and their data to the Web server as the entity content of the HTTP message, instead of as the URL address.

Parameters are passed using post

The amount of data transmitted by the get method is greater than that transmitted by the get method.

A large amount of data is transferred.

Instance:

// Post submit <br/> var postcontent = <br/> "name =" + encodeuricomponent ("Buford early ") + "& Email =" + encodeuricomponent ("buford@known-space.com"); <br/> var url = "www.baidu.com"; <br/> XMLHTTP. open ("Post", "somepage", true); <br/> XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded"); <br/> // send the form information in send <br/> XMLHTTP. send (postcontent );

In short, get

Small data volume, high processing efficiency, low security, will be cached, and post

Otherwise.

 

Use get

Note:

1 For get

Please
Evaluate (or if URL-based parameters are involved), the passed parameters must be processed by the encodeuricomponent method first. For example: var url =
"Update. php? Username = "+ encodeuricomponent (username) +" & content ="
+ Encodeuricomponent

(Content) + "& id = 1 ";

Use post

Note:

1. Set the context-type of the header to application/X-WWW-form-urlencode to ensure that the server knows that there are parameter variables in the object. Generally

SetRequestHeader ("context-type", "application/X-WWW-form-urlencoded;") of the XMLHTTPRequest object ;"). Example:

XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded ");

2. the parameter is a key-value pair with one-to-one correspondence between names and values. Each pair of values is separated by an ampersand. for example, VAR name = ABC & sex = Man & age = 18. Note that VaR name = Update. PHP?

ABC & sex = Man & age = 18 and VAR name =? ABC & sex = Man & age = 18 is incorrect;

3. the parameter is sent in the send (parameter) method, for example, XMLHTTP. Send (name); if it is get

Method, directly XMLHTTP. Send (null );

4. server-side Request Parameters differentiate get

And post

. For get

$ Username = $ _ Get ["username "];

Method, then $ username

= $ _ Post ["username"];

Related Article

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.