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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:15:46+00:00 2026-05-29T11:15:46+00:00

Just cant figure it out. I have a large object stored as a JSON

  • 0

Just cant figure it out. I have a large object stored as a JSON file and I want to access it once and use it multiple times:

var myjson = new Object();
$.getJSON("myJSON.js", function(json) {
  myjson = JSON.stringify(json);
});
$('#console').append(myjson);

This does nothing. It’s a scope issue, I know. I just don’t know how to do what I’m wanting. Must I do all my functions inside the $.getJSON call or is there a way to pass the object that I can use throughout runtime?

  • 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-29T11:15:47+00:00Added an answer on May 29, 2026 at 11:15 am

    It’s a scope issue, I know.

    No, it’s not a scoping issue. It’s an issue with your understanding about how AJAX works. AJAX is asynchronous. This means that when you send an AJAX request, the function that sent this request ($.getJSON in your case) returns immediately. It is only inside the success callback that you should use the results. This callback function could be called much later. It doesn’t really depend on you at what moment in time in the future this might happen. Might never happen actually if there is an error on your server.

    So the only place where you could reliably consume the results that the server sent after an AJAX call is inside the success callback:

    $.getJSON("myJSON.js", function(json) {
        // here and only here you can access the results of your AJAX call.
        $('#console').append(JSON.stringify(json));
    });
    

    There are some horrible things that you could do like performing synchronous calls to the server:

    var myjson = { };
    $.ajax({
        url: 'myJSON.js',
        dataType: 'json',
        async: false,
        success: function(json) {
            myjson = JSON.stringify(json);
        }
    });
    $('#console').append(myjson);
    

    The async: false option makes a synchronous call. Obviously this completely defeats the whole purpose of AJAX as it will freeze the browser during this call. You are probably better of directly including the myJSON.js as a script tag:

    <script type="text/javascript" src="myJSON.js"></script>
    

    Another benefit you might get from this approach is that the browser will ensure for you that this script is completely loaded before executing any other scripts. So the following could work fine:

    <script type="text/javascript" src="myJSON.js"></script>
    <script type="text/javascript">
        var myjson = someJavascriptVariableThatYouDeclaredInMyJSON;
    </script>
    

    and then later:

    <script type="text/javascript">
        $('#console').append(JSON.stringify(myjson));
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read a similar post, but i just cant figure out the problem.
I just cant figure out why am getting error 1064 from this query //prep
I just cant figure out how to get the output of a query and
I have this large XML file. There is a field that I want to
I just can't figure out how do I make sure an argument passed to
This really shouldn't be hard, I just can't figure out how to do it.
I'm pretty sure I'm having a CSS issue, but I just can't figure out
Ok, this is bugging me, and I just can't figure out what is wrong...
i know this is kinda retarded but I just can't figure it out. I'm
An easy problem, but for some reason I just can't figure this out today.

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.