In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.