Solved

What is wrong with this code?

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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…

730 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