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

  • Home
  • SEARCH
  • 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 6376009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:41:21+00:00 2026-05-25T01:41:21+00:00

Possible Duplicate: AJAX- response data not saved to global scope? I basically have a

  • 0

Possible Duplicate:
AJAX- response data not saved to global scope?

I basically have a loop that contacts a script on my site using AJAX and then updates a string with the response from that script. Here’s the code:

    // Image code array
    var result_url = 'http://localhost/view/';

    for(i = 0; i < urls.length; i++) {
        // Add URL to queue
        $('#url_queue').append('<div class="uploadifyQueueItem"><div class="cancel"><img src="/assets/img/cancel.png" /></div><span class="fileName">' + image_name_from_url(urls[i]) + '</span><div class="uploadifyProgress"><div class="uploadifyProgressBar"></div></div></div>');

        // Make a request to the upload script
        $.post('/upload', { url: urls[i], username: username }, function(response) {
            var response = jQuery.parseJSON(response);
            if(response.error) {
                alert(response.error);
                return;
            }
            if(response.img_code) {
                result_url += response.img_code + '&';
            }
        });
    }
    console.log(result_url);

The Firebug console just shows http://localhost/view/ when the string is logged. It’s like the img_code response from my upload script isn’t being appended to the string at all. I have tried logging the value of result_url within the $.post() method and that works fine, but the value is not being saved properly because it doesn’t show later in my code. Is this a scope problem? Will I have to define result_url as a global variable?

Thanks for any help.

  • 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-05-25T01:41:21+00:00Added an answer on May 25, 2026 at 1:41 am

    You are checking console.log(result_url); before the AJAX requests complete.


    AJAX requests are (by default) run asynchronously. What that means is that your script continues to run while the request is still being made to the server.

    Your callback function (provided to $.post as the 3rd parameter) is the one that get’s executed after your AJAX request has completed.


    Also note, that your AJAX request callback functions are called when the request is done. Your requests might not finish in the same order that they started. You could prevent all this by setting async:false, but that’ll halt all of your javascript execution.


    Another option would be to collect the jqXHR objects being returned by $.post, and then call $.when().done(), so that your console.log(result_url) happens only when all the AJAX requests are resolved:

    // Image code array
    var result_url = 'http://localhost/view/',
        jqHXRs = [];
    
    for(i = 0; i < urls.length; i++) {
        // Add URL to queue
        $('#url_queue').append('<div class="uploadifyQueueItem"><div class="cancel"><img src="/assets/img/cancel.png" /></div><span class="fileName">' + image_name_from_url(urls[i]) + '</span><div class="uploadifyProgress"><div class="uploadifyProgressBar"></div></div></div>');
    
        // Make a request to the upload script
        jqHXRs.push($.post('/upload', { url: urls[i], username: username }, function(response) {
            var response = jQuery.parseJSON(response);
            if(response.error) {
                alert(response.error);
                return;
            }
            if(response.img_code) {
                result_url += response.img_code + '&';
            }
        }));
    }
    
    $.when.apply(this, jqHXRs).done(function(){
        console.log(result_url);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jquery/ajax load new content when available i have a table named news
Possible Duplicate: How to: Back button support “Ajax” I have a ASP.NET MVC implementation
Possible Duplicate: What is a good jQuery/Ajax lightbox plugin? I'm not an JavaScript guy,
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: How to run a database script file from Delphi? I have a
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: jQuery: How to stop AJAX function escaping JSON string used to POST
Possible Duplicate: Resetting a multi-stage form with jQuery Once the form submitted, response from
Possible Duplicate: jquery validate & ajax.beginform I'm trying to use the jQuery validate plugin

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.