我正在為天氣日志應用程式撰寫代碼,事情是當我通過 fetch 方法獲取帶有郵政編碼和國家/地區代碼的 openweathermap.com 網站的 URL 時,它只接受國家/地區代碼,因為美國它從不接受我嘗試過的任何其他國家/地區這么多,但它從不接受其中任何一個,它只將預測的資料物件回傳給美國任何其他國家它只是顯示訊息未找到的城市是我的代碼:
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);
}
}
uj5u.com熱心網友回復:
我在其他國家作業得也很好。
const apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const zipCode = "110007";
const countryCode = "IN";
const endPointData = async (zipCode, countryCode, apiKey) => {
const URL = `https://api.openweathermap.org/data/2.5/weather?zip=${zipCode},${countryCode}&appid=${apiKey}`;
const res = await fetch(URL);
try {
const data = await res.json();
console.log(data);
}
catch(error){
// appropriately handles the errors.
console.log("error",error);
}
}
endPointData(zipCode, countryCode, apiKey);
uj5u.com熱心網友回復:
console.log 你的 URL 變數。也許你的國家代碼是空的。當未指定國家代碼時,API 默認回退到美國。 https://openweathermap.org/current#zip
先在郵遞員那里試試(https://www.postman.com/)!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/419790.html
標籤:
下一篇:如何在輸入元素中獲取圖示?
