0

I am trying to use ipinfo api in my django app.

my js code -

var button = document.getElementById('login_button');
var user_location = document.getElementById('user_location');
button.onclick=function()
{
    get_location();
    form.submit();
}   
function get_location()
{
    alert('inside');
    $.get('http://ipinfo.io', function(response)
        {
            alert('response');
            user_location.value = response.loc;
            console.log(response.loc);
            alert(response.loc);
        }
    ,'jsonp');
}

my html code(relevant)-

<div class="row">
            <form id="form" method="post" class="form">
                {% csrf_token %}
                <input type="hidden" id="user_location" name="user_location"/>
            </form>
        </div>
        <div class="row">
            <button type="button" id="login_button" name="confirm_login" class="tooltip-test btn btn-sm btn-success" title="Click to Log In">
                Login
            </button>
        </div>

and my views.py relevant code -

 o = online_status(username = u.username, location = post['user_location'])
                devices_no = 1
                o.save()

My problem starts with the js file. It is alerting 'inside' but niether it alerts 'response' nor there is any thing in my browser console and also there is no alert of response.loc.

Where I am wrong..??

Help me please.

Note: My internet is behind a proxy.

Thanks in advance.

aquaman
  • 1,523
  • 5
  • 20
  • 39
  • That site won't let you access its content from your page. The browser will prevent such attempts. – Pointy Mar 17 '15 at 19:38
  • And why is that..I followed the syntax given on the site itself... – aquaman Mar 18 '15 at 19:10
  • If you check the response headers from loading that URL, you won't see any "Access-Control-Allow-Origin" header. Without that, the browser won't let you see it from a different domain. – Pointy Mar 18 '15 at 23:38
  • http://ipinfo.io supports CORS, so that's unlikely to be the issue. You might be hitting a rate limit though. Try alerting or logging response, rather than response.loc – Ben Dowling Mar 19 '15 at 03:19
  • I don't think rate limit is an issue in this case...and about response, the function upon success is not running. There is no alert 'response'. Any other idea?? – aquaman Mar 20 '15 at 15:25
  • Why don't you think a rate limit is the issue? If you're calling this from localhost it almost certainly will be (lots of people call the API from localhost!). You should see an 429 error in the console. – Ben Dowling Mar 22 '15 at 18:03

1 Answers1

1

Most likely, you are hitting the "cross-origin problem". See here for more discussion.

Community
  • 1
  • 1
Michael Lorton
  • 43,060
  • 26
  • 103
  • 144