Home > Web Front-end > JS Tutorial > When to use asynchronous or synchronous AJAX

When to use asynchronous or synchronous AJAX

藏色散人
Release: 2019-01-22 14:39:54
Original
3486 people have browsed it

AJAX stands for Asynchronous JavaScript and XML and is a technology that allows web pages to be updated asynchronously, meaning that the browser does not need to reload the entire page when only a small amount of data on the page changes. AJAX only delivers updated information to the server.

When to use asynchronous or synchronous AJAX

Standard web applications handle interactions between web visitors and servers synchronously. This means one thing happens one after the other; the server does not multitask. If the button is clicked, a message is sent to the server and a response is returned. You cannot interact with any other page elements until the response is received and the page is updated.

Obviously, this delay will have a negative impact on the web visitor's experience - hence the use of AJAX.

What is AJAX?

AJAX is not a programming language, but a client-side script that integrates communication with the web server (i.e. in the user's browser technology of running scripts). Furthermore, its name is somewhat misleading: while an AJAX application might use XML to send data, it could also just use plain text or JSON text. But usually, it uses the XMLHttpRequest object in the browser (to request data from the server) and JavaScript to display the data.

AJAX: Synchronous or Asynchronous

AJAX can actually access the server both synchronously and asynchronously:

Synchronously, where the script stops and waits for the server to Send a reply before continuing.

Async, where the script is allowed to continue processing the page and handle replies when the page arrives.

Handling requests synchronously is similar to reloading the page, but only downloads the requested information rather than the entire page. Therefore, using AJAX synchronously is faster than not using it at all, but it still requires the visitor to wait for the download before any further interaction with the page. Typically, users know that they sometimes need to wait for pages to load, but are not used to experiencing constant, noticeable delays on a site.

Handling requests asynchronously avoids delays in retrieving from the server because the visitor can continue to interact with the web page; the requested information will be processed in the background, and the response will update the page as it arrives. Furthermore, even if the response is delayed (for example, in the case of very large data), the user may not be aware of it because they are taking up time elsewhere on the page. However, for most responses, the visitor is not even aware that a request was made to the server.

Therefore, the preferred way to use AJAX is to use asynchronous calls whenever possible. This is the default setting in AJAX.

Why use synchronous AJAX?

If asynchronous calls provide such an improved user experience, why does AJAX provide a way to make synchronous calls?

While asynchronous calls are the best option in most cases, there are rare cases where it doesn't make sense to allow visitors to continue interacting with the web page until a specific server-side process is completed.

In many cases it is better not to use Ajax at all and just reload the entire page. The synchronous option in AJAX is useful for the few cases where asynchronous calls cannot be used but without reloading the entire page. For example, you may need to handle some transactions where orders are important. Consider a situation where a web page needs to return a confirmation page after the user clicks on something. This requires a synchronous request.

The above is the detailed content of When to use asynchronous or synchronous AJAX. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template