1

I am facing a quite Strange problem, I dont seem to be able to override variable jSonData from inside the success function of $.get()

$.fn.name = function(data, options) {
        var jSonData = data;
        var settings = $.extend({
                ....        
        }, options);

        if(typeof data !== 'object') {
            if(settings.debug) console.log('Getting configuration settings from: ', data);
            $.get(data, function(d) {
                jSonData = d; //I attempt to override the value here
            }, 'json');
        }
        console.log(jSonData); // Gives the same value as it was before
};

Note: The success event of the $.get() is triggered

Starx
  • 77,474
  • 47
  • 185
  • 261
  • 1
    Isn't Ajax _asynchronous_? – Ram May 22 '13 at 00:26
  • @undefined It just slipped off my mind. Is there a way to solve this in a correct manner, without `setTimeOut()` or `setInterval()`? – Starx May 22 '13 at 00:31
  • @Starx: Check this question http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – elclanrs May 22 '13 at 00:32
  • @Starx: you haven't explained the original problem you're solving – zerkms May 22 '13 at 00:34
  • @zerkms, I am trying to override variable value from inside the $.get(). The question elclanrs posted answer my question. I have voted this to be closed. – Starx May 22 '13 at 00:38
  • "I am trying to override variable value from inside the $.get()" --- it's not the task, it's a weird solution. Task is what you want to achieve with this code. – zerkms May 22 '13 at 00:39
  • @zerkms, Oh! I am trying to create a way for JSON Object to either to be provided directly or loaded using a link which returns JSON output. – Starx May 22 '13 at 00:42

2 Answers2

1

By the time you logged the value, the $.get() has not overridden jSonData yet since the AJAX request has not returned by that time. Do the console.log inside the function instead.

$.get(data, function(d) {
  jSonData = d; //I attempt to override the value here - You just did!
  console.log(jSonData);
}, 'json');
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • Is there a way to solve this in a correct manner, without `setTimeOut()` or `setInterval()`? – Starx May 22 '13 at 00:31
  • @Starx: where do you see either `setTimeout` or `setInterval` in this answer? – zerkms May 22 '13 at 00:33
  • @Starx: so why did you mention them then? And why do you think the solution isn't correct? – zerkms May 22 '13 at 00:35
  • @zerkms, I have read about similar problem before, and the solutions were to check and wait for the data to return by the use of those functions. – Starx May 22 '13 at 00:37
  • @Starx: but this answer doesn't use it. It doesn't use hundreds of other functions available in javascript, and it's not the reason to mention all of them :-) – zerkms May 22 '13 at 00:38
  • @zerkms, No it does not, so I asked Joseph, if he knows a solution `Is there a way to solve this in a correct manner, without setTimeOut() or setInterval()?`. See? :P – Starx May 22 '13 at 00:43
  • @Starx: he **already** provided a solution that doesn't use them. His original answer is a correct and doesn't use those functions. – zerkms May 22 '13 at 00:50
  • @Starx Maybe you posted the wrong question. What do you hope to attain? Why not run code in the callback? – Joseph May 22 '13 at 00:51
0

I was having that problem because AJAX calls are asynchronous and thus the call would not have been completed when console.log() was executed.

Solution I used to fix my issue was by using deferring methods.

$.fn.name = function(data, options) {
    var jSonData = data;
    var settings = $.extend({
            ....        
    }, options);
    var getData = function() {
        if(typeof data !== 'object') {
            return $.get(data, 'json');
        }
        else { return jSonData; }
    };
    getData().done(function(result) {
        jSonData = result;
        console.log(jSonData); // Gives the same value as it was before
    }).fail(function() {
        //
    });

};
Starx
  • 77,474
  • 47
  • 185
  • 261