Search code examples
jqueryajaxgeolocation

jQuery AJAX Geolocation Not Working


I am using freegeoip's API to get the location of the visitor. I have tried putting the code in $(document).ready (without click event) and also in click event but in both cases, it seems as though the AJAX call doesn't run. I've got the code from Shopify's webpage but since it's just jQuery, it should work in any cases with jQuery.

Here's the fiddle

Here's the JavaScript code :

jQuery(document).ready(function(){

    jQuery('button').click(function(){
        jQuery.ajax( { 
          url: 'http://freegeoip.net/json/', 
          type: 'POST', 
          dataType: 'jsonp',
          success: function(location) {
            jQuery('#location').append(location.country_code);
          }
        } );
    });
});

Here's the simple HTML :

<button type="button">Button</button>
<div id="location"></div>

Does anyone have any idea what went wrong? I've spent hours checking it but couldn't spot any obvious problems. I've also tried using another API and $.getJSON but could never get it work. There was also no error message in the console. I just wish to run it after page load and upon success, execute some other functions.

Thanks for helping.


Solution

  • So the problem is finally identified. It's AdBlockPlus which has blocked freegeoip.net, that's why it doesn't work on this computer only (and I have that plugin installed for all my browsers).

    Hope this helps for anyone facing similar issues.