地理位置响应country_name在Safari中不起作用

时间:2017-10-23 19:06:41

标签: javascript jquery geolocation country-codes

我的地理定位响应country_name正在使用Chrome和Firefox,但不适用于Safari。我该如何解决这个问题?

使用Javascript:

$.get("http://freegeoip.net/json/", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#country_code").html(response.country_code);
    if(response.country_code=='NL'||response.country_code=='US'){
        document.getElementById(response.country_code).style.display = "block";
    }
}, "jsonp");

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="NL">THE NL</div>
<div id="US">THE US</div>

CSS:

#NL { display:none;} 
#US { display:none;} 

Demo

1 个答案:

答案 0 :(得分:1)

小提琴正在High Sierra的最新Safari中工作。我也在Chrome中检查了这个,但由于Ad Blocking插件阻止了对http://freegeoip.net/json/

的请求,因此无效

要解决此问题,您可以在后端创建代理(创建简单脚本,它将为您执行GET请求,因此客户端请求将与其原始请求相同)。

但您注意到的错误The page was not allowed to run insecure content from freegeoip.net/json与其他内容不同 - 您正在尝试请求取消对网站的保护(http://而不是https://)。

幸运的是,此服务也可以在https上运行,只需将s添加到链接即可。

$.get("https://freegeoip.net/json/", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#country_code").html(response.country_code);
    if(response.country_code=='NL'||response.country_code=='US'){
        document.getElementById(response.country_code).style.display = "block";
    }
}, "jsonp");