Solved

What is wrong with this code?

Posted on 2013-11-16
4
321 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
[X]
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
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)

695 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