Skip to main content

How to get json data from a file (jQuery)



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?


Comments

  1. 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>

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex