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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:43:46+00:00 2026-06-08T04:43:46+00:00

My function is very simple, and based on what I found here . Yet

  • 0

My function is very simple, and based on what I found here. Yet I cant seem to make it work, whats wrong?

function ajax(url) {
    var result;
    $.ajax({
        url: url,
        success: function(data) {
            result = data;
            //$('#test').html(data);
            }
        });
    return result;
    }

The function is getting the data, for if I uncomment the commented code and call it, then the data is shown inside the #test element. However, if I write

var test = ajax('my-url.php');
$('#test').html(test);

then it doesnt work. My guess is that for some reason, the data in “data” is not being stored in the variable “result”. But I cant figure out why, nor how to solve it. Any help is appreciated.

  • 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-08T04:43:48+00:00Added an answer on June 8, 2026 at 4:43 am

    The result variable is not being changed until the AJAX call finishes, by which point the ajax function has already returned. This is the entire point of callbacks, the ability to schedule a task to be executed when an event is finished.

    Javascript (in-browser) is inherently asynchronous.

    Alternatively, you can pass in async: false to your $.ajax call and it will wait until it’s finished, but reverting back to your original style makes more sense.

    Edit: It looks like what you’re doing can be accomplished with $.load().

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

Sidebar

Related Questions

I'm trying to implement a very simple shorthand function for sending headers, based on
I have this very simple jQuery function: $(.milestone-in-tree).live({ mouseenter: function() { setTimeout( $.ajax({ type:
A little context: I'm trying to make a very simple hashing function/hash table as
I have a very simple function that takes a list of comma separated (x,y)
I have written a very simple function: function editCategory() { $ID = urlencode($_GET['id']); $cname
I'm trying to define a very simple function in Drools as below: import java.util.List;
I've written a very simple select function for SQLite, but I'm confused at how
I have this very simple br2nl function that I use to take a string
I have a very simple CLR Function for doing Regex Matching public static SqlBoolean
I've got a very simple VBA function that takes two range arguments and returns

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.