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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:18:48+00:00 2026-05-17T15:18:48+00:00

I want to get the IP from the current user and send it along

  • 0

I want to get the IP from the current user and send it along with an AJAX POST to a PHP file. Which means I need to re-use the variable I get in the response from the IP .getJSON request.

Script 1: I found this handy snippet on snipt:

$.getJSON("http://jsonip.appspot.com?callback=?",function(data){
    alert( "Your ip: " + data.ip);
});

Script 1 works and gives an alert dialog with your IP.

Script 2: I converted script 1 into this:

 var ipAppspot;
 $.getJSON("http://jsonip.appspot.com?callback=?",function(data){
         ipAppspot = data.ip;
 });
 alert(ipAppspot);

As I understand that not declaring the ‘var’ keyword in front of a variable makes it avaible to all scopes even outside the function (global). Though I guess that should work if script 2 wouldn’t be a Cross-domain request maybe, but in this case it gives an alert dialog with ‘undifined’ So this example won’t work though

On Stackoverflow there is another question jQuery-storing-ajax-response-into-global-variable which deals with about the same problem.

And appending the solution they give I get the following.

Script 3: Appending the solution from the link

// 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());

Script 3 Gives the same problem as script 2, which is ‘undefined’

Question: How can I re-use this variable in later uses in my script?

EDIT for Nick Cravers answer

var ipAppspot;
$.getJSON("http://jsonip.appspot.com?callback=?",function(data){
  ipAppspot = data.ip;
  myFunc();       
});
function myFunc() {
  alert(ipAppspot);
}

by Nick Craver, which does in fact works.

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 + "&password=" + passwordRaw + "&ip=" + ip;
    // start the AJAX 
    $.ajax({
       type: "POST",
       url: phpFile,
       dataType: "json",
       data: dataConstruct,
       success: function(msg){
         alert(msg);
       }
     });
} // END dataAuth

This is in my application, and I would use the function as such:

$('body').delegate('#feg-submit', 'click', function(){
    $(this).attr('disabled','disabled');
    console.log('submit button clicked');
    dataAuth(ipAppspot);
    return false
});

​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?

  • 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-17T15:18:49+00:00Added an answer on May 17, 2026 at 3:18 pm

    It’s not a scoping issue, it’s a timing issue, let’s illustrate it with an example that does work, like this:

    var ipAppspot;
    $.getJSON("http://jsonip.appspot.com?callback=?",function(data){
      ipAppspot = data.ip;
      myFunc();       
    });
    function myFunc() {
      alert(ipAppspot);
    }
    

    You can test it here. Notice it does alert your IP, so what’s different here? You’re not asking for the variable (by calling alert()) until the variable is populated, and this happens in the $.getJSON() callback. So the scope isn’t an issue, but the fact that the callback runs later (when the response comes back) is an issue, just initiate whatever needs the data from the callback, since that’s the first time the data’s available.

    Here’s a more common example, passing the data directly to wherever it goes:

    $.getJSON("http://jsonip.appspot.com?callback=?",function(data){
      myFunc(data.ip);
    });
    function myFunc(ipAppspot) {
      alert(ipAppspot);
    }
    

    Or, the short version:

    $.getJSON("http://jsonip.appspot.com?callback=?", myFunc);
    function myFunc(ipAppspot) {
      alert(ipAppspot);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whenever I want to get data from a plist file I use the following
I want to get the UserID of the current user from whom the Workflow
I get ReffererUrl from current User, if refferer is exists i need to extract
I want to get text from current active line (the line where the caret
I want to get zip code from users current location(Latitude, Longitude), I had used
Using ASP.NET membership, if I want to get information for the current user, I
In my application I have get directions from the user's current location. When a
I want to get data from two tables. I have patient first name and
I want to get data from a table in my MySQL database. $linkID =
I want to get data from two tables with single Entity class. How?? public

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.