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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:37:55+00:00 2026-06-17T10:37:55+00:00

Possible Duplicate: How to return the response from an AJAX call from a function?

  • 0

Possible Duplicate:
How to return the response from an AJAX call from a function?

I have a function that reads numerical data from a file online. While the file is retrieved correctly (proven by the value displayed by the alert), when I try to set it to output and then return output, I get an undefined result.

I tried to go through scoping tutorials to no avail. What am I doing wrong?

function readFileFromWeb() {
    var output;

    jQuery.get('http://domain.com', function(data) {
        alert(data);
        output = data;
    });

    return output;
}
  • 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-17T10:37:56+00:00Added an answer on June 17, 2026 at 10:37 am

    Here’s how it should be done, via a callback function.

    function readFileFromWeb(callback) {
        jQuery.get('http://domain.com', function(data) {
            callback && callback(data);
        });
    }
    
    // ...
    
    readFileFromWeb(function (data) {
        alert(data);
    });
    

    This is because jQuery.get is an asynchronous function i.e. it doesn’t block while executing and thus returns immediately. And that is infact the reason why the jQuery.get function requires a callback function, so that it fires your callback function once the AJAX query has returned.

    Thus, you will need to use the same callback pattern when implementing your own readFileFromWeb function.

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

Sidebar

Related Questions

Possible Duplicate: How to return the response from an AJAX call from a function?
Possible Duplicate: How to return the response from an AJAX call from a function?
Possible Duplicate: How to return AJAX response Text? How to return the response from
Possible Duplicate: jQuery: Return data after ajax call success I wrote a script which
Possible Duplicate: VB.NET Function Return If I have a function that returns a boolean,
Possible Duplicate: Return Value from inside of $.ajax() function I'm working on a CakePHP
Possible Duplicate: Ajax cross domain call I have Asp.Net Controller methods which return JSON
Possible Duplicate: AJAX- response data not saved to global scope? I basically have a
Possible Duplicate: Can’t get correct return value from an jQuery Ajax call How to
Possible Duplicate: return AJAX callback return I have made a JQuery code using AJAX,

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.