Solved

What is wrong with this code?

Posted on 2013-11-16
4
316 Views
Last Modified: 2013-11-18
I include this in one of the client side Javascripts:

    function getIP() {
        var ipAddress = 'Unknown IP';
        $.ajax({
            dataType: "json",
            url: "http://smart-ip.net/geoip-json?callback=?",
            async: false,
            success: function(data) {
                ipAddress = data.host;
            }
        });
        return ipAddress;
    }

Open in new window


It is executed from a click button event and always returns 'Unknown IP'. If I test the URL in my browser, I get the correct json reply.
0
Comment
Question by:itnifl
  • 2
  • 2
4 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39653148
Check the documentation : http://api.jquery.com/jQuery.ajax/

async (default: true)
Type: Boolean
By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.

You're using JSONP (callback=?)
You're doing a cross-domain requests (http://smart-ip.net)

What about : http://jsfiddle.net/mB22D/

function getIP() {
    var ipAddress = "Unknown IP";
    return $.getJSON("http://smart-ip.net/geoip-json?callback=?");
}
getIP().then(function(info) {
    alert(info.host);
});

Open in new window

0
 
LVL 2

Author Comment

by:itnifl
ID: 39655609
In your code, you have no other code that continues while the request is being made. Your request will be made asynchronously and the variable ipAddress will not contain the client IP when it is returned:

var ipAddress = 'Unknown IP';
function getIP() {
    return $.getJSON("http://smart-ip.net/geoip-json?callback=?");
}
getIP().then(function(info) {    
    ipAddress = info.host;
});
alert(ipAddress);

Open in new window


This is why I want to execute the code synchronously so that the code will wait until the fetch for information is done and THEN return the result.

http://jsfiddle.net/mB22D/1/
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39656104
It's just like you did not read my previous comment...
Anyway here a working code with your way of think : http://jsfiddle.net/mB22D/2/

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>GET IP</title>
<script>
    var ipAddress = 'Unknown IP';
    function saveIP(info) {
        ipAddress = info.host;
    }
    function getIP() {
        return ipAddress;
    }
</script>
<script src="http://smart-ip.net/geoip-json?callback=saveIP"></script>
<script>
    alert(getIP());
</script>
</head>
<body>
</body>
</html>

Open in new window

0
 
LVL 2

Author Closing Comment

by:itnifl
ID: 39656110
OK - I see, thank you very much ! :D
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now