Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the difference between ajax request post and get and the choice of get post

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand the difference between ajax request post and get and the choice of get post". The content of the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to understand the difference between ajax request post and get and the choice of get post"!

The simplest difference:

1. When using a Get request, the parameters are displayed in URL, but not in Post mode

two。 Using Get request to send a small amount of data, Post request to send a large amount of data

3.get requests need to pay attention to cache problems, but post requests do not need to worry about this problem.

Get mode:

Simple data can be transmitted by get, but the size is generally limited to 1KB, and the data is appended to the url (http's header transfer), that is, the browser appends each form field element and its data to the resource path in the request line in the format of the URL parameter. In addition, the most important point is that it is cached by the client's browser so that others can read the customer's data, such as account number and password, from the browser's history. Therefore, in some cases, the get method can cause serious security problems.

Post mode:

When using POST mode, 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 of the URL address. The amount of data transmitted by POST is much larger than that transmitted by GET.

When using get, you should pay attention to:

For get requests (or those involving url passing parameters), the passed parameters should be processed by the encodeURIComponent method first. Example: var url = "update.php?username=" + encodeURIComponent (username) + "& content=" + encodeURIComponent

(content) + "& id=1"

That is, the passed parameters of get need to be spliced into the url.

When using Post, you should note:

1. Set the Context-Type of header to application/x-www-form-urlencode to ensure that the server knows that there are parameter variables in the entity. The SetRequestHeader ("Context-Type", "application/x-www-form-urlencoded;") of the XmlHttpRequest object is usually used. Example:

XmlHttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded")

two。 Parameters are key-value pairs corresponding to name / value, and each pair of values is separated by an & sign. Such as var name=abc&sex=man&age=18, pay attention to var name=update.php?

Abc&sex=man&age=18 and var name=?abc&sex=man&age=18 are both written incorrectly.

3. Parameters are sent in the Send (parameter) method, for example: xmlHttp.send (name); if it is in get mode, direct xmlHttp.send (null)

4. Server-side request parameters distinguish between Get and Post. In get mode, $username = $_ GET ["username"]; if in post mode, $username = $_ POST ["username"]

The passing parameters of post do not need to be spliced into url

Get method is received by Request.QueryString ["strName"]

Post method is received by Request.Form ["strName"]

Note:

Although the two submission methods can use Request ("strName") to obtain the submission data, it has an impact on the efficiency of the program and is not recommended.

In general, try to avoid using Get to submit forms, as it may lead to security problems

AJAX garbled problem

The cause of garbled code:

1. The default character encoding for the data returned by xtmlhttp is utf-8. If the client page is gb2312 or other encoded data, garbled codes will be generated.

2. The default character encoding of the data submitted by the post method is utf-8, which will generate garbled code if the server is gb2312 or other encoded data.

The solutions are:

1. If the client is gb2312 encoded, specify the output stream encoding on the server.

2. Both server and client use utf-8 encoding

Gb2312:header ('Content-Type:text/html;charset=GB2312')

Utf8:header ('Content-Type:text/html;charset=utf-8')

Note: if you have followed the above method or returned garbled code, check whether your method is get. For get requests (or anything involving url passing parameters), the passed parameters should be handled by the encodeURIComponent method first. If there is no encodeURIComponent processing, it will also produce garbled code.

When to use Get requests and when to use Post requests

The purpose of the Get request is to give the server some parameters to get the list from the server. For example: list.aspx?page=1, which means to get the data on the first page

The purpose of the Post request is to send some parameters to the server, such as the contents of the form.

The following example is used to show the difference between a Get request and an Post request when sending the same piece of data.

GET is simpler and faster than POST, and works in most cases.

However, use the POST request in the following cases:

Cannot use cache files (update files or databases on the server)

Send a large amount of data to the server (POST has no data limit)

POST is more stable and reliable than GET when sending user input containing unknown characters

Thank you for your reading, the above is the content of "how to understand the difference between ajax request post and get and the choice of get post". After the study of this article, I believe you have a deeper understanding of how to understand the difference between ajax request post and get and the choice of get post, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report