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 8132967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:28:26+00:00 2026-06-06T09:28:26+00:00

I am using JQuery $.ajax to loop through a set of JSON URLs and

  • 0

I am using JQuery $.ajax to loop through a set of JSON URLs and download results into an array. Some of the URLs return a 404 — which I’m handling & showing as a div message.

However, I can’t seem to pass the URL, more precisely it always only passes the last URL in the array.

I assume that’s because ajax is asynchronous and takes longer to complete, but I’m not sure how else to make sure only the current JSON url (or variable) is being shown on SUCCESS or ERROR

My Code:

 // For every URL loop through 'baseitems' array
 for (var i = 0; i < baseitems.length; i++) {

  // This is where I'm hoping to store the current URL as a variable to pass to the end-user on Success or Error
  var caturl = baseURL + "/" + baseitems[i];

  // Get the data via JSON
  $.ajax({
    type: "GET",
    url: caturl,
    dataType: "json",
    async: true, // set so that the variables caturl get updated below
    success: function (result) {
        // Success: store the object into catalog array
        cat.unshift(result);
        $('#showdata').prepend("Loaded: " + caturl + "</br>");  // still buggy here - probably async JSON issue
    },
    error: function (xhr, textStatus, error) {
        // Error: write out error
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
        $('#showdata').prepend("ERROR : '" + error + "' trying to access: " + caturl + "</br>");  // still buggy here  - probably async JSON issue
    }
});

}

** UPDATE: WORKING CODE **

The completed working code with @charlietfl help + a few nice things like Success / Error code + count of URLs loaded is below. Thanks charlietfl & peacemaker!

                $.ajax({
                    type: "GET",
                    url: caturl,
                    dataType: "json",
                    async: true, // set so that the variables caturl get updated below
                    beforeSend: function (jqXHR, settings) {
                        /* add url property and get value from settings (or from caturl)*/
                        jqXHR.url = settings.url;
                    },
                    success: function (result, textStatus, jqXHR) {
                        // Success: store the object into catalog array
                        var url = jqXHR.url;
                        cat.unshift(result);
                        $('#showdata').prepend("<font size=\"1\">Loading: " + url + " status: " + textStatus + "</font></br>");
                        successcount += 1;

                    },
                    /* error to be deprecated in jQuery 1.8 , superseded by "fail" */
                    error: function (jqXHR, textStatus, error) {
                        var url = jqXHR.url;
                        /* replace caturl with url in your append */
                        $('#showdata').prepend("<font size=\"1\" color=\"red\">ERROR : '" + error + "' trying to access: " + url + "</font></br>");
                    },
                    complete: function (jqXHR, textStatus) {
                        $('#showdata').prepend("<font size=\"3\">Loaded <b>" + successcount + "</b> of " + baseitems.length + " total catalogs.</font></br>")
                    }
                });
  • 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-06T09:28:29+00:00Added an answer on June 6, 2026 at 9:28 am

    Here is one way. The beforeSend callback option gives you access to both the jqXHR object and the ajax settings object.

    You won’t be able to use caturl in the append of error, as it will not be in synch with the request throwing error.

      $.ajax({
        /* url, data ...& other opts*/
        beforeSend:function( jqXHR, settings){
            /* add url property and get value from settings (or from caturl)*/
             jqXHR.url= settings.url;
       },
       /* error to be deprecated in jQuery 1.8 , superseded by "fail" */
       error: function(jqXHR, , textStatus, error){
           var url=jqXHR.url;
         /* replace caturl with url in your append */ 
         $('#showdata').prepend("ERROR : '" + error + "' trying to access: " + url + "</br>");
       }
    

    EDIT: based on 3rd party API comments, it is important to recognze following taken straight from $.ajax API

    When data is retrieved from remote servers (which is only possible using the script or jsonp data types), the error callbacks and global events will never be fired.

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

Sidebar

Related Questions

I am using jQuery ajax to pass a string array to a page method.
I'm having a problem with jQuery/Ajax/JSON. I'm using a jQuery ajax post like so...
Javascript/JQuery noob here, so apologies. I'm using .ajax to get a JSON object then
I'm building a guestlist app for my company using PHP, Javascript/jQuery/Ajax, JSON and localstorage.
I am looping through json return values with jQuery's $.each() and adding the classes
I am sending a json string using JQuery.ajax to a php page where I
I'm working on a site that involves looping through an array, using a jQuery
I am using JQUERY .ajax() to send serialized form data to MySQL using PHP.
I'm having trouble using jQuery.ajax() to post a gist to Github. The gist is
I recently started using jQuery/AJAX for submitting data to my PHP API. Currently what

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.