【发布时间】:2017-10-30 09:03:03
【问题描述】:
我正在本地开发环境 (Ubuntu 16.04) 中开发一个网站,并通过 http://localhost.example/ 在 Chrome (58) 上测试该网站 - 它连接到本地 Web 服务器。
运行这个 Javascript:
$(document).ready(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
});
触发此错误:
[弃用] getCurrentPosition() 和 watchPosition() 不再起作用 关于不安全的起源。要使用此功能,您应该考虑 将您的应用程序切换到安全源,例如 HTTPS。看 https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins了解更多详情。
这是为什么呢?我了解面向公众的网站需要运行 HTTPS 才能使地理定位库/功能正常工作。我们有许多通过 HTTPS 运行类似代码的公共网站。
不过根据depreciation documentation:
localhost 被视为 HTTP 上的安全源,所以如果你能 要从 localhost 运行您的服务器,您应该能够测试 该服务器上的功能。
上面的 Javascript 在通过 http://localhost.example/test-page/ 加载的 HTML 正文中内联运行 - 那么为什么我在 Chrome 中收到“不安全的来源”错误?
Firefox (53) 按预期显示浏览器访问位置提示。
【问题讨论】:
-
你能在本地机器上使用 https 运行它吗?
-
可能是因为
localhost!==localhost.example -
此外,Firefox 55 版也将具有 https 限制 - 在 firefox 中
Locally delivered files such as http://localhost and file:// paths are considered to have been delivered securely.- 因此,它在 fox 中的工作方式与 chrum 相同 -
@JaromandaX - 我可以确认localhost/test-page 有效。如果您想将其添加为答案,我将选择正确。
标签: javascript google-chrome geolocation google-geolocation