【问题标题】:Enable HTTP Geolocation For Local Firefox Debugging为本地 Firefox 调试启用 HTTP 地理位置
【发布时间】:2018-03-17 18:11:26
【问题描述】:

“地理定位请求只能在安全上下文中完成”

地理位置需要 HTTPS 很好,但我需要在不安全的本地服务器上进行调试,然后才能使其生效。

我希望我可以编辑 Firefox 的“about:config”以禁用此保护措施以进行调试。我该怎么做?在不安全的上下文中是否有其他/更好的方法来调试位置? (模拟 https)

【问题讨论】:

  • this:medium.freecodecamp.org/… 听起来需要做很多工作,但如果你真的需要它,你可以试试 ;) 但我实际上想知道.. 我在 localhost 上有一个带有地理位置的项目,它实际上可以工作例如在 Firefox 开发人员中

标签: javascript jquery firefox geolocation secure-context


【解决方案1】:

你可以在不加密的本地主机上测试

Firefox 不会通过非安全连接发送地理定位,并且无法禁用此行为。但是,localhost 被认为是安全连接,因此在测试时可能是一个选项。这也解释了为什么 Christopher Supertramp 可以通过 http 尝试他的代码——他在 localhost 上。

这是来自 Mozilla 文档:

http://localhost 和 file:// 路径等本地传递的文件被视为已安全传递。

https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts

【讨论】:

  • 欢迎来到 SO!即使你的答案是正确的,也试着解释一下。此外,您正在支持一个可以消失的外部链接,因此最好解释一下您的答案,以防万一消失。
  • 如果主机名是 localhost,这可能会起作用,但是如果您使用映射到 127.0.0.1 的不同主机名运行呢?
【解决方案2】:

我想知道为什么它仍然对我有用,但这是我的代码:

    function geolocate() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position) {
                let lat = position.coords.latitude;
                let lng = position.coords.longitude;
                var geolocation = {
                    lat: lat,
                    lng: lng
                };
                document.getElementById('lat').value = lat;
                document.getElementById('lng').value = lng;
                console.log("----------------------------------------------");
                console.log("Found Location: "+ lat + " / " + lng);

                var google_maps_geocoder = new google.maps.Geocoder();
                google_maps_geocoder.geocode(
                    { 'latLng': geolocation },
                    function( results, status ) {
                        let street = results[0].address_components[1].long_name;
                        let number = results[0].address_components[0].long_name;
                        let plz = results[0].address_components[6].long_name;
                        let city = results[3].address_components[0].long_name;
                        //let country = results[7].formatted_address;
                        let full = street+" "+number+", "+plz+" "+city;

                        // write the values in the fields
                        document.getElementById('autocomplete').value = full;
                        document.getElementById('route').value = street;
                        document.getElementById('street_number').value = number;
                        document.getElementById('postal_code').value = plz;
                        document.getElementById('locality').value = city;
                        //document.getElementById('country').value = country;
                        console.log("User Address: "+street+" "+number+", "+plz+" "+city);
                        $.ajax({
                            type: 'POST',
                            url: 'include/set-location.inc.php',
                            data: {lat: lat, lng: lng, street: street, number: number, plz: plz, city: city, full: full},
                            dataType: 'json',
                            success: function(response) {
                                if(response.status === 'success') {
                                    console.log("Saved address in a cookie!");
                                    if (site === "jobs") {
                                        location.reload(true);
                                    }
                                }
                            }
                        });
                    }
                );
            });
      }
    }

浏览器“问题”:

我的控制台:

希望我的代码对你有所帮助;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-28
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2012-08-11
    • 2019-06-23
    相关资源
    最近更新 更多