我正在使用本地開發環境(Ubuntu 16.04)在網站上工作,並通過http://localhost.example/在Chrome上測試網站(58) - 它連接到本地Web服務器。爲什麼我在本地主機上的Chrome中出現getCurrentPosition()和watchPosition()「不安全起源」錯誤?
運行此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才能使地理位置庫/功能正常工作。我們有許多公共網站通過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)按預期方式顯示瀏覽器訪問位置提示。
您可以使用https運行它嗎? – ControlAltDel
也許是因爲'localhost'!==''localhost.example' –
也是,Firefox從版本55開始也會有https限制 - 在firefox中''本地交付的文件如http:// localhost和file://路徑被認爲是已經被安全地傳遞了。「 - 因此,它將和狐狸一樣工作 –