Computer User
Computer User

Reputation: 2879

jQuery shows JSON result in browser but does not show result

I have a code that gives geolocation of an IP address:

// set endpoint and your access key
var ip = '134.201.250.155';
var access_key = '771d15ae7a7e93262786cc48009bac80';
console.log('http://api.ipstack.com/' + ip + '?access_key=' + access_key);
// get the API result via jQuery.ajax
jQuery.ajax({
    url: 'http://api.ipstack.com/' + ip + '?access_key=' + access_key,   
    dataType: 'jsonp',
    success: function(json) {

        console.log(json);
 
        // output the "capital" object inside "location"
        console.log(json.location.capital); 

    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

The URL is: http://api.ipstack.com/134.201.250.155?access_key=771d15ae7a7e93262786cc48009bac80

It runs fine in browser and gives output in JSON format. But the alert does not execute in the code. I don't know why.

Please note that the service is free and the access_key is free for all, so there is no security concern.

Note: I gave up and used another service: https://ipapi.co/json/ This URL is working fine and giving me correct result in both console and code. Thanks guys for all your support.

Upvotes: 0

Views: 178

Answers (2)

filipe
filipe

Reputation: 2047

You have to use https api.ipstack endpoint because your site is https and browser security does not allow you to use http calls on https websites.

I tried https call to ipstack but your subscription does not support it.

You have to pay for it.

Upvotes: 1

Hasta Dhana
Hasta Dhana

Reputation: 4719

Actually the ajax call is failed, you could check it by using the .fail() error handler : https://jsfiddle.net/Lf6v7xcp/

Upvotes: 1

Related Questions