我的地理定位响应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;}
答案 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");