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
// http://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?