[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

What is wrong with this code?

Posted on 2013-11-16
4
Medium Priority
?
327 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…
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)
Suggested Courses

650 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