The loop solution in Ajax and the Ajax loop solution

Source: Internet
Author: User

The loop solution in Ajax and the Ajax loop solution

Ajax Introduction

Ajax consists of HTML, JavaScript™The combination of technology, DHTML and DOM can transform clumsy Web interfaces into interactive Ajax applications. The author of this article is an Ajax expert who demonstrates how these technologies work collaboratively-from an overview to a detailed discussion-to make efficient Web development a reality. He also unveiled the secrets of Ajax core concepts, including XMLHttpRequest objects.

Five years ago, if you do not know XML, you are an ugly duckling that no one cares about. Eighteen months ago, Ruby became the focus of attention. programmers who did not know Ruby had to sit on the bench. Today, if you want to keep up with the latest technology, your goal is Ajax.

However, Ajax is not just a fashion, it is a powerful way to build websites, and it is not as difficult as learning a brand new language.

I. Business Requirements

During development, when a list page is loaded, I need to obtain the corresponding data from the server based on the id of each item in the list, and then assign the obtained data to the tag corresponding to the current id.

See the following table for example:

I have a series of product numbers. I need to use ajax to get the product name on the server based on the product number, and then use js to update the interface (the actual business is certainly not as simple as obtaining the product name)

II. Implementation Scheme

2.1 error plan

Generally, we directly think of writing a for loop, initiating an ajax request in the loop to obtain data, and then updating the obtained data to the tag corresponding to the corresponding id,

As follows:

We use arrays to simulate some column IDs:

var array = [1, 3, 2, 5, 3];

Cyclic ajax request method:

function foreach_ajax() {for (var i = 0; i < array.length; i++) {$.get("/home/loop_ajax", { value: array[i] }, function (data) {console.log(array[i]+","+data);});}}

Call:

$(function () {foreach_ajax(); });

The test results are as follows:

We can see that we cannot get the value of array [I] Within the loop.

The reason for this is that ajax is executed asynchronously. At the end of the loop, ajax does not return server data for the first time. At the end of the loop, the variable I in for has been released, so array [I] = undefined

2.2 correct solution

The correct method is to loop ajax recursively.

As follows:

We use arrays to simulate some column IDs:

var array = [1, 3, 2, 5, 3];

Recursive ajax request method:

function Loop_ajax(index, array) {if (index < array.length) { var value = array[index];$.get("/home/loop_ajax", { value: value }, function (data) {console.log(array[index] + "," + data);if (index < array.length) {Loop_ajax(index + 1, array); }});}}

Call:

$(function () {Loop_ajax(0, array);});

The test results are as follows:

The above is a small Editor to introduce you to the Ajax loop solution, I hope to help you, if you have any questions, please leave a message, the small editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.