对于学校作业,我们必须构建一个与 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/