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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:45:52+00:00 2026-05-29T07:45:52+00:00

I have the following javascript code: function initSite(){ var site; $.getJSON(www+’init/initSite’, function(data) { site

  • 0

I have the following javascript code:

function initSite(){
    var site;
    $.getJSON(www+'init/initSite', function(data) { site = data; });
}
$(document).ready(function(){
    var site = initSite();
        console.log(site);
}

which returns undefined… how can i store the json object that i recieve in the site variable so i can use it later?

EDIT:
This seem to work but im not sure if its correct to use this solution

var site = null;
$.ajax({
  url: www+"init/initSite",
  async: false,
  dataType: 'json',
  success: function (data) {
    site = data;
  }
});
console.log(site);
  • 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-29T07:45:53+00:00Added an answer on May 29, 2026 at 7:45 am

    of course you got undefined because your function doesn’t return anything and the ajax call is also asynchronous, so you have to wait the server response. Since $.ajax (and shortcuts) returns a promise you can do this task using deferred

    function initSite(){
        return $.getJSON(www+'init/initSite');
    }
    $(document).ready(function(){
        $.when(initSite()).done(function(data) {
            /* continue here the code execution, e.g. call another function */
    
            doAllTheRemainingWorkWith(data)
    
        });
    }
    

    as you can see this code is short and easy to read

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

Sidebar

Related Questions

I have the following code: <script type=text/javascript> $(document).ready(function() { $(#Save).click(function() { $.post(url, { data:
I have the following Javascript code: function checkIfValid(){ var form = document.createuserform; var valid
I have the following Javascript code: function checkIfValid(){ var form = document.createuserform; if(form.fName.value ==
So I have the following JavaScript code: <script type=text/javascript> function clearRadioButtons() { document.getElementById(radiobutton1).checked=; //etc
I have the following JavaScript code: var ans_el = document.createElement( 'input' ); ans_el.setAttribute( 'id',
I have the following javascript code. var Test = function(){ $(#+elementId).click(function(){ // How to
I have the following JavaScript code: var cILo=true; var img1=images/title-2a.png; var img2=images/title-2b.png; function loadblinker()
I have the following javascript code: <script type=text/javascript> $(function () { var currentDateTime =
I have the following JavaScript code: Link In which the function makewindows does not
I have the following Javascript code add_num = { f: function(html, num) { alert(this.page);

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.