【问题标题】:Geolocation response country_name doesn't work in Safari地理位置响应 country_name 在 Safari 中不起作用
【发布时间】:2017-10-23 19:06:41
【问题描述】:

我的地理位置响应 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

【问题讨论】:

  • 你有 safari 的广告拦截插件吗?
  • 没有有效的广告拦截器。但是,如果我检查该元素,Safari 会给出以下错误:[警告] [阻止] 该页面不允许运行来自 freegeoip.net/json 的不安全内容

标签: javascript jquery geolocation country-codes


【解决方案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");

【讨论】:

  • 没问题,但正如我所指出的,请注意,使用广告屏蔽的用户也会遇到类似的问题。克服这个问题的一种可能性是向您的后端发出请求,该请求将为您执行该请求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 1970-01-01
相关资源
最近更新 更多