Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

What is wrong with this code?

Posted on 2013-11-16
4
319 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

809 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