我正在本地开发环境 (Ubuntu 16.04) 中开发一个网站,并通过 http://localhost.example/ 在 Chrome (58) 上测试该网站- 连接到本地网络服务器。
运行此 JavaScript:
$(document).ready(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
});
触发此错误:
[Deprecation] getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins for more details.
这是为什么呢?我了解面向公众的网站需要运行 HTTPS 才能使地理定位库/功能正常工作。我们有许多公共(public)网站通过 HTTPS 运行类似的代码。
但是根据 depreciation documentation :
localhost is treated as a secure origin over HTTP, so if you're able to run your server from localhost, you should be able to test the feature on that server.
上面的 Javascript 在通过 http://localhost.example/test-page/ 加载的 HTML 正文中内嵌运行。 - 那么为什么我在 Chrome 中收到“不安全来源”错误?
Firefox (53) 按预期显示浏览器访问位置提示。
最佳答案
Chrome 认为 http 上的 localhost 是安全的。由于您通过 http 使用 hostnme localhost.example ,因此这不被认为是安全的。
注意:Firefox 的行为与 Firefox 55 类似
关于javascript - 为什么我在本地主机上的 Chrome 中收到 getCurrentPosition() 和 watchPosition() "insecure origins"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44252897/