Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:45:06+00:00 2026-06-13T14:45:06+00:00

When i running Ajax request in loop, ajax on success does not displays returned

  • 0

When i running Ajax request in loop, ajax on success does not displays returned data…
Here is code:

url = ["../siteAdmin/statistics/queriesAjax.php", "../siteAdmin/statistics/geographyAjax.php"];
tabContainerId = ['tabs-2', 'tabs-3'];

for(var i=0, len=url.length; i < len; i++){
    console.log(tabContainerId[i]);
    jQuery("#"+tabContainerId[i]).html('<img src="../assets/images/loading.gif"/>');
    jQuery.ajax({
        type:"GET",
        url:url[i],
        data:{ from:from, to:to },
        success: function (msg) {
            jQuery("#"+tabContainerId[i]).html(msg);
        },
        error: function (msg) {
            showError("Error occurred.", tabContainerId[i]);
        }
    });
}

In HTML element i just see loading.gif.
Console.log(msg) on ajax success in browser console are displayed. But in HTML element- not.
If i set var i = 0 or var i = 1 data is displayed.
Where i made a mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:45:07+00:00Added an answer on June 13, 2026 at 2:45 pm

    You’re using the variable i within your success callback, but by the time your callbacks are executed, i has almost certainly changed.

    You have to force i to be part of the callback function’s closure. One way to do this is to put the contents of the for loop into an immediately invoked function expression (IIFE):

    url = ["../siteAdmin/statistics/queriesAjax.php", "../siteAdmin/statistics/geographyAjax.php"];
    tabContainerId = ['tabs-2', 'tabs-3'];
    
    for(var i=0, len=url.length; i < len; i++) {
        (function(idx) {
            console.log(tabContainerId[idx]);
            jQuery("#"+tabContainerId[idx]).html('<img src="../assets/images/loading.gif"/>');
            jQuery.ajax({
                type:"GET",
                url:url[idx],
                data:{ from:from, to:to },
                success: function (msg) {
                    jQuery("#"+tabContainerId[idx]).html(msg);
                },
                error: function (msg) {
                    showError("Error occurred.", tabContainerId[idx]);
                }
            });
        })(i);
    }
    

    In effect, this “saves” the value of i so it is the same when your callback functions use it.

    I haven’t run this code through lint or an interpreter, so there may be syntax errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an ajax file running the following code: $model = Mage::getModel('catalog/product'); //getting product
I am making an ajax request using JQuery that looks like this: var data
I'm running an AJAX request from a JavaScript-powered (+jQuery) webpage every 5 seconds for
Here is a simple snipped to do an Ajax request var req = new
I have the code below that grabs some html from an AJAX request and
Can we make Ajax request to a different instance of server(different port) running on
I'm trying to do an ajax request to get the contents of http://localhost/ running
I'm trying to run a function before async ajax request. However function is running
I occasionally have some long running AJAX requests in my Wicket application. When this
I have an app that sends multiple Ajax requests simultaneously. I was originally running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.