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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:52:13+00:00 2026-06-17T07:52:13+00:00

I have the following JavaScript code where I’m trying to get some data from

  • 0

I have the following JavaScript code where I’m trying to get some data from an action in a WebAPI project. The problem I’m having is that when I go to use the dataset variable, I get an error saying that dataset is undefined. Also, I would expect my alert No. 1 to fire first, but the alert No. 2 fires first, with the dataset as undefined, then the alert No. 2 fires, and it then contains my data. What am I doing wrong here?

<script type="text/javascript">
    $().ready(function() {
        var dataset;
        $.get("http://localhost:9619/api/values", function(data) {
            dataset = data;
            alert("No. 1 " + dataset);
        });

        alert("No. 2 " + dataset);
    });
</script>
  • 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-17T07:52:15+00:00Added an answer on June 17, 2026 at 7:52 am

    $.get() is asynchronous. So the execution of the callback is delayed until the request has finished.

    The execution order in your case is something like this:

    1. start the get request
    2. alert( "No. 2 " );
    3. [request has finished] execute the callback:  alert( "No. 1 " );
    

    You should add all code reliant on the $.get() request to the respective callback. That way you make sure the request has succeed and your variables are set.

    Another less preferable solution would be to use $.ajax() and set its async parameter to false. But in that case, the execution of all JavaScript code waits until your request finishes and your page freezes in the mean time.

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

Sidebar

Related Questions

I have the following JavaScript code to get contact name and thumbnail from the
I have the following Javascript code, and I'm trying to get a callback to
Hi I have following JavaScript code that I am trying to run. My aim
I have the following javascript code, which is modified from the SimpleUpload demo in
I have the following javascript code: function changeButtonState(targetSelector, action, iconClass) { var $target =
I have the following javascript code that is trying to call a server side
I have the following javascript code: $.get(categories/json_get_cities/ + stateId, function(result) { //code here },
I have the following JavaScript code: alert(data.status); data is a JSON object, one of
I have the following javascript code: function initSite(){ var site; $.getJSON(www+'init/initSite', function(data) { site
I have the following JavaScript code that gets the Id property (Guid) from every

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.