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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:49:33+00:00 2026-05-24T08:49:33+00:00

I have a function that makes a jQuery Ajax call to the BART api

  • 0

I have a function that makes a jQuery Ajax call to the BART api to get the series of next trains coming in. The function should return a string of the next train departures:

function getNextTrain(station)
{   
    var returnString = "";
    var title = station.title;

    $.ajax({
        type:"GET",
        url:"http://api.bart.gov/api/etd.aspx?cmd=etd&orig="+title+"&key=2BDD-I3UG-U3L6-ZNK2",
        dataType:"xml",
        success: function(xml){

            $(xml).find("estimate").each(function(){
                returnString += "-------------------<br>"
                               +"Train: " + $(this).find("color").text() + "<br>"
                               +"Direction: " + $(this).find("direction").text() + "<br>"
                               +"ETD: " + $(this).find("minutes").text() + " minutes<br>";
            });
        // console.log(returnString);   
        }
    }); 
    return returnString;
 }

if I dump the string to the console just after the $(xml).find(“estimate”).each(… ‘s closing brace (currently commented out) – the string is correct:

-------------------<br>Train: BLUE<br>Direction: South<br>ETD: 4 minutes<br>------------`-------<br>Train: GREEN<br>Direction: South<br>ETD: 12 minutes<br>-------------------<br>Train: BLUE<br>Direction: South<br>ETD: 19 minutes<br>-------------------<br>Train: BLUE<br>Direction: North<br>ETD: 11 minutes<br>-------------------<br>Train: BLUE<br>Direction: North<br>ETD: 26 minutes<br>-------------------<br>Train: BLUE<br>Direction: North<br>ETD: 41 minutes<br>-------------------<br>Train: GREEN<br>Direction: North<br>ETD: 3 minutes<br>-------------------<br>Train: GREEN<br>Direction: North<br>ETD: 18 minutes<br>-------------------<br>Train: GREEN<br>Direction: North<br>ETD: 33 minutes<br>-------------------<br>Train: RED<br>Direction: South<br>ETD: 8 minutes<br>-------------------<br>Train: RED<br>Direction: South<br>ETD: 23 minutes<br>-------------------<br>Train: RED<br>Direction: South<br>ETD: 37 minutes<br>-------------------<br>Train: YELLOW<br>Direction: North<br>ETD: 14 minutes<br>-------------------<br>Train: YELLOW<br>Direction: North<br>ETD: 29 minutes<br>-------------------<br>Train: YELLOW<br>Direction: North<br>ETD: 44 minutes<br>-------------------<br>Train: RED<br>Direction: North<br>ETD: 6 minutes<br>-------------------<br>Train: RED<br>Direction: North<br>ETD: 22 minutes<br>-------------------<br>Train: RED<br>Direction: North<br>ETD: 37 minutes<br>-------------------<br>Train: YELLOW<br>Direction: South<br>ETD: Arrived minutes<br>-------------------<br>Train: YELLOW<br>Direction: South<br>ETD: 15 minutes<br>-------------------<br>Train: YELLOW<br>Direction: South<br>ETD: 30 minutes<br>`

but when I return it (or log it where the return statement is), I get an empty string.
I tried:
-declaring a global variable outside of the function
-using the concat() function
-returning at the point where I log the string successfully

No matter what I do, when I view the return value in the calling function, I get an empty string. I have been blocked for hours, and I don’t know what I am doing wrong. I appreciate 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-24T08:49:33+00:00Added an answer on May 24, 2026 at 8:49 am

    .success is not called immediately. It is called only when your Ajax request comes back succesfully. You need to have a global variable and set it from the success callback for it to work.

    Better (since global variables are evil), use a callback:

    function printListofStations(list) {
       console.log(returnString);  
    }
    
    function getNextTrain(station, printListofStations)
    {   
        var returnString = "";
        var title = station.title;
    
        $.ajax({
            type:"GET",
            url:"http://api.bart.gov/api/etd.aspx?cmd=etd&orig="+title+"&key=2BDD-I3UG-U3L6-ZNK2",
            dataType:"xml",
            success: function(xml){
    
                $(xml).find("estimate").each(function(){
                    returnString += "-------------------<br>"
                                   +"Train: " + $(this).find("color").text() + "<br>"
                                   +"Direction: " + $(this).find("direction").text() + "<br>"
                                   +"ETD: " + $(this).find("minutes").text() + " minutes<br>";
                    printListofStations(returnString);
                });
    
            }
        }); 
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that makes a series of calls to sscanf() and then,
I have a python function that makes a subprocess call to a shell script
I'm running a weird issue here. I have code that makes jquery ajax calls
So, I have a page that loads and through jquery.get makes several requests to
I have a javascript function that calls a generic function to make an ajax
I have a function which makes an AJAX request to a server and returns
Ok, so I have a JQuery AJAX call in an .html file, sitting on
I need some jQuery ajax help. I currently have a main content div that
I have been struggling to get my jquery call to a webmethod to work.
I have a spinner that displays when an ajax request is made using jquery:

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.