Javascript getCurrentPosition() 和 watchPosition() 在不安全的来源上被弃用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32106849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getCurrentPosition() and watchPosition() are deprecated on insecure origins
提问by Jason Axelrod
I am getting this error on my website which requests Geolocation data from the user:
我在我的网站上收到此错误,该错误要求用户提供地理位置数据:
getCurrentPosition() and watchPosition() are deprecated on insecure origins, and support will be removed in the future. You should consider switching your application to a secure origin, such as HTTPS. See goo.gl/rStTGzfor more details.
getCurrentPosition() 和 watchPosition() 在不安全的来源上已被弃用,未来将取消支持。您应该考虑将您的应用程序切换到安全来源,例如 HTTPS。有关更多详细信息,请参阅goo.gl/rStTGz。
I mean its basically just a notice, and the google link just says its being deprecated.
我的意思是它基本上只是一个通知,而谷歌链接只是说它被弃用了。
I have no plans on moving my website to SSL... so is there an alternative for someone like me?
我没有将我的网站迁移到 SSL 的计划......那么像我这样的人有替代方案吗?
采纳答案by kittimiyo
Found a likely answer in /jstillwell's posts here: https://github.com/stefanocudini/leaflet-gps/issues/15basically this feature will not be supported (in Chrome only?) in the future, but only for HTTP sites. HTTPS will still be ok, and there are no plans to create an equivalent replacement for HTTP use.
在 /jstillwell 的帖子中找到了一个可能的答案:https: //github.com/stefanocudini/leaflet-gps/issues/15基本上将来不会支持此功能(仅在 Chrome 中?),但仅适用于 HTTP 站点。HTTPS 仍然可以,并且没有计划为 HTTP 使用创建等效的替代品。
回答by gogson
Because switching to HTTPS can be painful or impossible depending on your architecture, I found a workaround solution: you can use the Google Maps Geolocation API. Although it has usage limits, it does the job. You will need an browser API key, so don't forget to limit it's usage to your page hostname.
因为根据您的架构,切换到 HTTPS 可能会很痛苦或不可能,所以我找到了一个变通的解决方案:您可以使用Google Maps Geolocation API。虽然它有使用限制,但它可以完成工作。您将需要一个浏览器 API 密钥,因此不要忘记将其使用限制为您的页面主机名。
I use it as a fallback method to the getCurrentPosition()
method if it fails. It allows me to make it work until I switch to HTTPS.
getCurrentPosition()
如果失败,我将其用作该方法的后备方法。它允许我让它工作,直到我切换到 HTTPS。
Here's the JSFiddles:
这是 JSFiddles:
回答by Asim K T
Yes. Google Chrome has deprecated the feature in version 50. If you tried to use it in chrome the error is:
是的。谷歌浏览器已弃用版本 50 中的功能。如果您尝试在 chrome 中使用它,错误是:
getCurrentPosition() and watchPosition() are deprecated 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.
getCurrentPosition() and watchPosition() are deprecated 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.
So, you have to add SSL certificate. Well, that's the only way.
因此,您必须添加 SSL 证书。嗯,这是唯一的方法。
And it's quite easy now using Let's Encrypt. Here's guide
现在使用Let's Encrypt非常容易。这是指南
And for testing purpose you could try this:
出于测试目的,您可以尝试以下操作:
1.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.
2.You can run chrome with the --unsafely-treat-insecure-origin-as-secure="http://example.com" flag (replacing "example.com" with the origin you actually want to test), which will treat that origin as secure for this session. Note that you also need to include the --user-data-dir=/test/only/profile/dir to create a fresh testing profile for the flag to work.
1.localhost 被视为 HTTP 上的安全来源,因此如果您能够从 localhost 运行您的服务器,您应该能够在该服务器上测试该功能。
2.您可以使用 --unsafely-treat-insecure-origin-as-secure="http://example.com" 标志运行 chrome(用您实际想要测试的来源替换“example.com”),将在此会话中将该源视为安全的。请注意,您还需要包含 --user-data-dir=/test/only/profile/dir 以创建新的测试配置文件以使该标志起作用。
I think Firefox also restricted user from accessing GeoLocation API requests from http
. Here's the webkit changelog: https://trac.webkit.org/changeset/200686
我认为 Firefox 还限制用户访问来自http
. 这是 webkit 更新日志:https: //trac.webkit.org/changeset/200686
回答by Ben Dowling
You could use the https://ipinfo.ioAPI for this (it's my service). It's free for up to 1,000 req/day (with or without SSL support). It gives you coordinates, name and more. Here's an example:
您可以为此使用https://ipinfo.ioAPI(这是我的服务)。每天最多 1,000 个请求(无论是否支持 SSL)都是免费的。它为您提供坐标、名称等。下面是一个例子:
curl ipinfo.io
{
"ip": "172.56.39.47",
"hostname": "No Hostname",
"city": "Oakland",
"region": "California",
"country": "US",
"loc": "37.7350,-122.2088",
"org": "AS21928 T-Mobile USA, Inc.",
"postal": "94621"
}
Here's an example which constructs a coords object with the API response that matches what you get from getCurrentPosition()
:
这是一个示例,它使用与您从中获得的内容相匹配的 API 响应构造一个 coords 对象getCurrentPosition()
:
$.getJSON('https://ipinfo.io/geo', function(response) {
var loc = response.loc.split(',');
var coords = {
latitude: loc[0],
longitude: loc[1]
};
});
And here's a detailed example that shows how you can use it as a fallback for getCurrentPosition()
:
这是一个详细的示例,展示了如何将其用作以下各项的后备getCurrentPosition()
:
function do_something(coords) {
// Do something with the coords here
}
navigator.geolocation.getCurrentPosition(function(position) {
do_something(position.coords);
},
function(failure) {
$.getJSON('https://ipinfo.io/geo', function(response) {
var loc = response.loc.split(',');
var coords = {
latitude: loc[0],
longitude: loc[1]
};
do_something(coords);
});
};
});
See http://ipinfo.io/developers/replacing-navigator-geolocation-getcurrentpositionfor more details.
有关更多详细信息,请参阅http://ipinfo.io/developers/replacing-navigator-geolocation-getcurrentposition。
回答by Archil Labadze
It's only for test, you can do it in google chrome:
navigate to: chrome://flags/#unsafely-treat-insecure-origin-as-securethen you'll see:
Type address you want to allow, then enableand relaunchyour browser.
它仅用于测试,您可以在 google chrome 中进行:导航到:chrome://flags/#unsafely-treat-insecure-origin-as-secure然后您会看到:
键入您要允许的地址,然后启用并重新启动浏览器。
回答by Injection
You can run chrome with the --unsafely-treat-insecure-origin-as-secure="http://example.com" flag (replacing "example.com" with the origin you actually want to test), which will treat that origin as secure for this session. Note that you also need to include the --user-data-dir=/test/only/profile/dir to create a fresh testing profile for the flag to work.
您可以使用 --unsafely-treat-insecure-origin-as-secure="http://example.com" 标志(用您实际想要测试的源替换“example.com”)运行 chrome,它将处理该来源对于本次会议来说是安全的。请注意,您还需要包含 --user-data-dir=/test/only/profile/dir 以创建新的测试配置文件以使该标志起作用。
For example if use Windows, Click Start and run.
例如,如果使用 Windows,请单击“开始”并运行。
chrome --unsafely-treat-insecure-origin-as-secure="http://localhost:8100" --user-data-dir=C:\testprofile
回答by Paul
For dev only, you can authorize specific local domains to use this features:
仅对于开发人员,您可以授权特定的本地域使用此功能:
回答by srmilon
In HTTP the error occurs.
在 HTTP 中发生错误。
Set permission for localhostin bellow label(Accept requests from these HTTP referrers (web sites)).
在波纹管标签中为localhost设置权限(接受来自这些 HTTP 引用(网站)的请求)。
It worked for me.
它对我有用。
回答by Jeffery ThaGintoki
I know that the geoLocation API is better but for people whom can't use an SSL, you can still use some sort of services such as geopluginService.
我知道 geoLocation API 更好,但对于不能使用 SSL 的人,您仍然可以使用某种服务,例如geopluginService。
as specified in the documentation you simply send a request with the ip to the service url http://www.geoplugin.net/php.gp?ip=xx.xx.xx.xx
the output is a serialized array so you must need to unserialize it before using it.
如文档中所述,您只需将带有 ip 的请求发送到服务 urlhttp://www.geoplugin.net/php.gp?ip=xx.xx.xx.xx
输出是一个序列化数组,因此您必须在使用它之前对其进行反序列化。
Remember this service is not very accurate as the geoLocation is, but it is still an easy and fast solution.
请记住,此服务不像 geoLocation 那样准确,但它仍然是一个简单快捷的解决方案。
回答by Edris
Use FireFox
or any other browser instead of Chrome
if you want to test it on your development environment, for production there is no way except using https
.
如果您想在开发环境中测试它,请使用FireFox
或任何其他浏览器Chrome
,对于生产,除了使用https
.
For development environment just open http://localhost:8100/
on FireFox and alas no such error.
对于开发环境,只需http://localhost:8100/
在 FireFox 上打开,可惜没有这样的错误。