javascript - 为什么我在本地主机上的 Chrome 中收到 getCurrentPosition() 和 watchPosition() "insecure origins"错误?

标签 javascript google-chrome geolocation google-geolocation

我正在本地开发环境 (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/

相关文章:

javascript - 禁用 vuetify 中 v-autocomplete 中的选定选项

javascript - Chrome 不返回哈希值

python - 无法从节点连接 : A device attached to the system is not functioning error using ChromeDriver Selenium on Windows OS 读取描述符

android - 在 Android 5+ 上使用地理定位插件时速度为 “null”

javascript - Uncaught ReferenceError : lat is not defined

javascript - HTML 未读取外部 Javascript 文件

javascript - 如何使用 .split 防止在引号逗号处拆分?

android - Cordova 后台地理定位支持,应用程序不会在一段时间后终止

javascript - 对定制内置元素的测试支持

javascript - Protractor - 启动 chrome 以禁用 cors 的网络安全