title: jQuery (cross domain) .getJSON, store response in re-usable variable
body: <p>I want to get the <em>IP</em> from the current user and send it along with an <em>AJAX POST</em> to a <em>PHP</em> file. Which means I need to re-use the variable I get in the response from the IP .getJSON request.</p>
  
  <p><strong>Script 1:</strong> I found this handy snippet on <a href="http://snipt.net/robertbanh/get-client-ip-through-jquery/" rel="nofollow noreferrer">snipt</a>:</p>
  
  <pre><code>$.getJSON("http://jsonip.appspot.com?callback=?",function(data){
      alert( "Your ip: " + data.ip);
  });
  </code></pre>
  
  <p><em>Script 1</em> works and gives an alert dialog with your <em>IP</em>.</p>
  
  <p><strong>Script 2:</strong> I converted <em>script 1</em> into this:</p>
  
  <pre><code> var ipAppspot;
   $.getJSON("http://jsonip.appspot.com?callback=?",function(data){
           ipAppspot = data.ip;
   });
   alert(ipAppspot);
  </code></pre>
  
  <p>As I understand that not declaring the <em>'var'</em> keyword in front of a variable makes it avaible to all scopes even outside the function <em>(global)</em>. Though I guess that should work if <em>script 2</em> wouldn't be a Cross-domain request maybe, but in this case it gives an alert dialog with <em>'undifined'</em> <strong>So this example won't work though</strong></p>
  
  <p>On <em>Stackoverflow</em> there is another question <a href="https://stackoverflow.com/questions/905298/jquery-storing-ajax-response-into-global-variable">jQuery-storing-ajax-response-into-global-variable</a> which deals with about the same problem.</p>
  
  <p>And appending the solution they give I get the following.</p>
  
  <p><strong>Script 3:</strong> Appending the solution from the link</p>
  
  <pre><code>// https://stackoverflow.com/questions/905298/jquery-storing-ajax-response-into-global-variable
  var ipStore = (function(){
      var ipAppspot;
      $.getJSON("http://jsonip.appspot.com?callback=?",function(data){ipAppspot = data.ip;});
      return {getIp : function()
      {
          if (ipAppspot) return ipAppspot;
          // else show some error that it isn't loaded yet;
      }};
  })();
  
  alert(ipStore.getIp());
  </code></pre>
  
  <p><em>Script 3</em> Gives the same problem as <em>script 2</em>, which is <em>'undefined'</em></p>
  
  <p><strong>Question:</strong> How can I re-use this variable in later uses in my script?</p>
  
  <p><strong>EDIT for Nick Cravers answer</strong></p>
  
  <pre><code>var ipAppspot;
  $.getJSON("http://jsonip.appspot.com?callback=?",function(data){
    ipAppspot = data.ip;
    myFunc();       
  });
  function myFunc() {
    alert(ipAppspot);
  }
  </code></pre>
  
  <p><em>by Nick Craver</em>, which does in fact works.</p>
  
  <pre><code>function dataAuth(ip){      
      // collect data from form
      var usernameRaw = $('#feg-username').val();
      var passwordRaw = $('#feg-password').val();
      // phpFile is our back-endgine
      var phpFile = 'feg/back-endgine.php';
      // construct the JSON in a variable
      var dataConstruct   = "username=" + usernameRaw + "&amp;password=" + passwordRaw + "&amp;ip=" + ip;
      // start the AJAX 
      $.ajax({
         type: "POST",
         url: phpFile,
         dataType: "json",
         data: dataConstruct,
         success: function(msg){
           alert(msg);
         }
       });
  } // END dataAuth
  </code></pre>
  
  <p>This is in my application, and I would use the function as such:</p>
  
  <pre><code>$('body').delegate('#feg-submit', 'click', function(){
      $(this).attr('disabled','disabled');
      console.log('submit button clicked');
      dataAuth(ipAppspot);
      return false
  });
  </code></pre>
  
  <p>​So I would use the ipAppspot there, and I would need the IP for AJAX requests later on, is there any way I could implement it?</p>