javascript - "Navigator is not defined"JSHint,带有 JSON 的预测 API

标签 javascript json api adobe-brackets navigator

对于学校作业,我们必须构建一个与 Forecast.io API 接口(interface)的小型应用程序原型(prototype)。我对 API 的经验很少,但代码中的所有内容似乎都很干净而且没问题。

但是,在 Brackets 中,JSHint 不断告诉我“Navigator not Define (W117)”。此外,console.log 语句在多个功能中不起作用,并且浏览器不会要求我共享我的位置。我尝试复制确切的代码并得到相同的结果。

文件中的控制台日志都没有返回任何内容,我无法检查我的浏览器是否获取位置数据。

//JSON declareren
(function () {
    'use strict';
    /*global $, jQuery, console, alert*/
    //Variabelen invoegen
    var latitude, longitude, url, App, myApp, day, days, month, months, year, years, dateTime, getTest, date, Location, myLocation;


App = function () {
    this.getLocation = function () {
        if (navigator.geolocation) {
            console.log("Geofix werk");
            navigator.geolocation.getCurrentPosition(this.updateWeather);
        } else {
            alert("Geolocation fix fail");

        }
    };

    this.updateWeather = function (posn) {
        console.log("Position OK");
        latitude = posn.coords.latitude;
        longitude = posn.coords.longitude;
        console.log(latitude);
        console.log(longitude);

        url = "https://api.forecast.io/forecast/412e0bc4cc3095a2de7d0bdf663f4e3e/" + latitude + ',' + longitude + "?units=si";

        $.ajax({
            url: url,
            dataType: 'jsonp',
            type: 'GET',
            success: function (resp) {
                console.log(resp);
                date = new Date();
                getTest = date.getDate();
                day = date.getDay();
                month = date.getMonth();
                year = date.getFullYear();

                //arrays invoegen
                days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

                months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

            }
        });
    };
};

myLocation = new App();
myLocation.getCurrentLocation();

}());

最佳答案

如果将 navigator 添加到 /*global ... 列表中,JSHint 错误应该会消失。

尝试将底部的代码更改为 myLocation.getLocation();,以便它正确匹配您在上面创建的函数名称 (this.getLocation = ...) >).

此外,您还混合使用了 alert()console.log()。不要忘记 console.log() 不会像 alert() 那样显示弹出消息。您需要打开浏览器的开发人员工具才能查看控制台消息。打开的开发工具还会告诉您代码中可能发生的其他错误,因此在测试代码时随时保持它们打开是一个好习惯。

关于javascript - "Navigator is not defined"JSHint,带有 JSON 的预测 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28962693/

相关文章:

javascript - 如果前四个字符是数字,则在字符串中插入字符

javascript - AngularJS:从另一个 Controller 或 JS 调用 Controller 更新功能

javascript - 从 JSON 中提取特定值

javascript - Dymo JavaScript API 可以连接到托管在同一网络上不同机器上的 Dymo Web 服务吗?

c++ - C++ API 中的渐进式披露

javascript - javascript中不同对象数组的一种排序函数

javascript - 从其他页面单击时 anchor 链接的滚动顶部偏移不起作用

c - 序列化时需要JSON

JSON结构输出

python - Flask-restful : How to only response to requests come with ('Accept' :'application/json' )?