错误是这样的:
Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
当前代码:
window.addEventListener('load', () => {
const locatoin = document.querySelector('#location');
const tempNum = document.querySelector('#temp');
const description = document.querySelector('#description');
const APIkey = 'myAPIkeyHere';
var lat;
var long;
var currentPos;
if (navigator.geolocation) {
currentPos = navigator.geolocation.getCurrentPosition(position => {
lat = position.coords.latitude;
long = position.coords.longitude;
});
const api = `api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${APIkey}`;
fetch(api)
.then(response => {
return response.json();
})
.then(data => {
console.log(data);
})
}
})
function refresh() {
location.reload();
}
在此之前,我没有得到任何错误,如果我手动搜索API url请求,它会给出json数据。
谢谢。
发布于 2020-09-27 00:26:47
您需要更新api url。它应该以https/http依赖于服务器开头,这样您将得到json响应,而不是html。
const api = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=${APIkey}`;
https://stackoverflow.com/questions/64079933
复制相似问题