Get the App
SLTechnology News&Howtos  ›  Development  › 

How to change asynchronous request to synchronous by ajax

Shulou Source: shulou.com Published: 2022-06-02 01:55:43 10月05日 Update

This article mainly explains "how ajax changes asynchronous requests to synchronous". The content of the explanation 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 ajax changes asynchronous requests to synchronous".

AJAX can be divided into synchronous and asynchronous according to the async value, which by default is true (asynchronous commit); to change async to synchronous, you just need to set the value of async to false.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

AJAX is divided into synchronous (async = false) and asynchronous (async = true) according to the value of async.

By default, async is true (asynchronous commit).

If you want to synchronize async, set it to false.

Example:

When using AJAX, we occasionally encounter the need to get an array and data corresponding to id from one interface, and then get the data on another interface, which is initially written as follows:

$.get (url_1, function (data) {var dom = []; for (var I = 0; I < data.length; iTunes +) {var item = data [I]; / / two url are inconsistent, look up another table $.get (url_2, function (data) {var item_result = data) according to id Dom.push ("item_result.id");});} $("# id") .empty () .append (dom.join ('');})

But at this time, there is often the problem that the data is not written after the array is emptied, and the beginner often mistakenly thinks that the interface is wrong, but it is not.

This is because the Ajax methods such as $get () take time to call the interface, resulting in the behavior that append has already occurred before push completes, that is, the array is emptied at this time, but there is no data read by join in the dom array at this time.

Therefore, you need to modify the Ajax in the loop to synchronize, and the code is modified as follows:

Get (url_1, function (data) {var dom = []; for (var I = 0; I < data.length; iTunes +) {var item = data [I]; / / change the synchronization $.ajaxSettings.async = false asynchronously before the second Ajax / / if the two url are inconsistent, look up another table $.get according to id (url_2, function (data) {var item_result = data; dom.push ("item_result.id");}) / notice that after the push in ajax is completed, change it back to asynchronous $.ajaxSettings.async = true;} $("# id") .empty () .append (dom.join ('));}) Thank you for reading, the above is the content of "how ajax changes asynchronous requests to synchronous". After the study of this article, I believe you have a deeper understanding of how ajax changes asynchronous requests to synchronous, 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!

Tags: Synchronization interface data array situation problem learning different consistent two content code actually not writing that is regular meetings ideas tutorials articles methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Xiaomi macOS OPPO Reno