Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the GET and POST methods of XMLHttpRequest

Shulou Source: shulou.com Published: 2022-06-01 04:50:03 09月20日 Update

This article mainly explains "how to use the GET and POST methods of XMLHttpRequest". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use XMLHttpRequest's GET and POST methods.

GET is simpler and faster than POST and can be used in most cases.

However, always use POST in the following situations:

Cache file is not an option (update a file or database on the server)

Send large amounts of data to the server (POST has no size limit)

Send user input (can contain unknown characters), POST is more powerful and more secure than GET

GET request

A simple GET request:

Example

Xhttp.open ("GET", "demo_get.asp", true)

Xhttp.send ()

In the above example, you might get a cached result. To avoid this, add a unique ID to URL:

Example

Xhttp.open ("GET", "demo_get.asp?t=" + Math.random (), true)

Xhttp.send ()

If you need to use the GET method to send information, add this information to URL:

Example

Xhttp.open ("GET", "demo_get2.asp?fname=Bill&lname=Gates", true)

Xhttp.send ()

POST request

A simple POST request:

Example

Xhttp.open ("POST", "demo_post.asp", true)

Xhttp.send ()

To POST data like a HTML form, add a HTTP header through setRequestHeader (). Specify the data you need to send in the send () method:

Example

Xhttp.open ("POST", "ajax_test.asp", true)

Xhttp.setRequestHeader ("Content-type", "application/x-www-form-urlencoded")

Xhttp.send ("fname=Bill&lname=Gates")

Method description

SetRequestHeader (header, value)

Add a HTTP header to the request

Header: specify the header name

Value: specify header value

At this point, I believe you have a deeper understanding of "how to use XMLHttpRequest's GET and POST methods". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Method instance header data information content situation file server cache learning service practical powerful deeper security example interest name size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux MariaDB Huawei NVidia