【发布时间】:2022-01-24 19:49:55
【问题描述】:
我正在为天气日志应用程序编写代码。问题是,当我将 openweathermap.com 网站的 URL 传递给 fetch 方法时,它使用邮政编码和国家/地区代码方式,它只接受国家/地区代码,因为美国它从不接受另一个国家/地区。我尝试了很多,但它从不接受其中任何一个,它只将预测的数据对象返回到美国,而对于任何其他国家,它只显示消息未找到城市。这是我的代码:
const generate = document.getElementById('generate').addEventListener('click', function(){
const zip = document.getElementById('zip').value;
const countryCode = document.getElementById('countryCode').value;
endPointData(zip,countryCode,apiKey);
})
const endPointData = async (zip,countryCode,apiKey) => {
const res = await fetch(`https://api.openweathermap.org/data/2.5/weather?zip=${zip},${countryCode}&appid=${apiKey}`);
try{
const data = await res.json();
console.log(data);
}
catch(error){
// appropriately handles the errors.
console.log("error",error);
}
}
const postData = async (url='', data = {}) => {
const response = await fetch(url,{
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
});
try{
const newData = await response.json();
return newData;
}catch(error){
console.log("error",error);
}
}
【问题讨论】:
标签: javascript html css node.js server