For details about GET and POST in Ajax getting started, ajaxget

Source: Internet
Author: User
Tags html header

For details about GET and POST in Ajax getting started, ajaxget

In the previous article, I summarized a method that is compatible with different browsers to create XHR objects with a nostalgic attitude:

After setting up the XHR object, what the client needs to do is to pass the data to the server in a certain way to get the corresponding response. Here, the second quarter of Ajax technology summary, I will focus on two ways to submit data.

Before that, you need to understand our HTTP transmission protocol:

HTTP works through the request-response protocol between the client and the server.

For example, the client (browser) submits an HTTP request to the server, and the server returns a response to the client. The response contains the Request status information and content that may be requested. To transmit data based on HTTP, two request methods are required.

Two HTTP request methods: GET and POST

The two most commonly used methods for request-response between the client and the server are GET and POST.

  • GET-request data from the specified resource.
  • POST-submit the data to be processed to the specified resource

This is the W3C usage scenario for GRT and POST. Literally, GET is used to obtain data from the server, and POST is used to transmit data to the server.

In this regard, we can see from the URL of the submission path and data:

Attributes that can be used to point to a URL include:

1. action in the form;

2. href in tag

3. src attribute in img script (this attribute is not restricted by the "same-origin policy" and can be used for "cross-origin". I would like to summarize a question about cross-origin in the near future. I will dig a hole here)

Here, let's talk about their differences in form submission.

I. In Ajax form submission, get uses the open () function to submit data. Where, the data uses URL? The key & value format is spliced after the URL:

xhr.open('get','xxx.php?name=tom & age=18');xhr.send(null);

In the browser URL, it is like this:

Get submit URL

Here we can see that GET adds the parameter data queue to the URL referred to by the action attribute of the submission form. The values correspond to each field in the form one by one, which can be seen in the URL. The url Length of the ID is limited. When the url is too long, the extra long characters are automatically intercepted. This will easily lead to a problem: when too many parameters are passed, resulting in excessive url length, the url automatically intercepts ultra-long characters, and the final passing parameter cannot be obtained. This also limits the size of data transmitted by GET, which generally cannot exceed 2 kb;

In addition, we can see from the URL that the GET security is very low. When you submit data using the GET method, the user name and password will appear on the URL. If:

-The login page can be cached by the browser;
-Other people can access the customer's machine.

Then, others can read the customer's account and password from the browser's history. Therefore, in some cases, the GET method may cause serious security problems.

It does not mean that the GET method has no advantages. In the speed test, the get submission speed is dozens of times higher than that of the POST method.

2. In Ajax form submission, POST only needs to provide the URL in the open () function, and the data is submitted by the send () function:

// Obtain form data var formDom = document. querySelector ('form'); var formData = new FormData (formDom); // send data xhr. open ('post', formDom. action); xhr. send (formData );

POST is: by using the HTTPPOST mechanism, each field in the form and its content are placed in the html header and transmitted to the URL address referred to by the action attribute. You cannot see this process. High Security

The amount of data transmitted by POST is large, which is generally not restricted by default. You can use the FormData object in this demo to transfer images, rich text, and other files. This is not what get can do.

To sum up, Get is a request to request data from the server, while Post is a request to submit data to the server. In FORM, the default Method is "GET ",

In essence, GET and POST are only different sending mechanisms, not a single sending!

In short, there are no advantages or disadvantages of the two form submission methods, but there are only different adaptation scenarios, which requires us to grasp according to requirements in our daily work.

Later, I will summarize several different writing methods of paths in the interaction between the browser and the server.

The above is a detailed description of the differences between GET and POST in the Ajax getting started section. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.