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 use the open and send methods of XMLHttpRequest

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of "how to use the open and send methods of XMLHttpRequest". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the open and send methods of XMLHttpRequest" can help you solve the problem.

Url-Files on the server

The url parameter of the open () method is the address of the file on the server:

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

This file can be any type of file, such as .txt and .xml, or server script files, such as .asp and .php, which can perform operations on the server before sending back the response.

Asynchronous-ture or false?

To send the request asynchronously, the async parameter of the open () method must be set to true:

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

Sending asynchronous requests is a huge step forward for web developers. Many of the tasks performed on the server are time-consuming. Prior to AJAX, this action might cause the application to hang or stop.

By sending asynchronously, JavaScript does not have to wait for a response from the server, but can:

Execute other scripts while waiting for the server to respond

Process the response when it is ready

Onreadystatechange attribute

Through the XMLHttpRequest object, you can define the function to execute when a request receives a reply.

This function is defined in the onreadystatechange property of the XMLHttpResponse object:

Example

Xhttp.onreadystatechange = function () {

If (this.readyState = = 4 & & this.status = = 200) {

Document.getElementById ("demo") [xss_clean] = this.responseText

}

}

Xhttp.open ("GET", "ajax_info.txt", true)

Xhttp.send ()

You will learn more about onreadystatechange in later chapters.

Synchronization request

To perform a request for synchronization, set the third parameter in the open () method to false:

Xhttp.open ("GET", "ajax_info.txt", false)

Sometimes async = false is used for quick testing. You will also see synchronization requests in older JavaScript code.

Because the code will wait for the server to complete, the onreadystatechange function is not required:

Example

Xhttp.open ("GET", "ajax_info.txt", false)

Xhttp.send ()

Document.getElementById ("demo") [xss_clean] = xhttp.responseText

Synchronized XMLHttpRequest (async = false) is not recommended because JavaScript stops execution until the server is ready to respond. If the server is busy or slow, the application will hang or stop.

Synchronous XMLHttpRequest is being removed from the Web standard, but the process may take many years.

Modern development tools are encouraged to warn against the use of synchronous requests and may throw InvalidAccessError exceptions when this occurs.

This is the end of the introduction to "how to use the open and send methods of XMLHttpRequest". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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