你的天气应用出了什么问题?

huangapple go评论65阅读模式
英文:

What am I doing wrong with my weather app?

问题

我正尝试创建一个天气应用程序,以学习编程。我自己设计了这个应用程序,并按照JavaScript教程进行了操作,但在使用OpenWeather API时遇到了400错误。以下是我的代码:

const searchbar = document.querySelector('#search-bar');
const temperature = document.querySelector('.temperature');
const description = document.querySelector('.description');
const imageContainer = document.querySelector('.image-container');
const API_KEY = '<MY_API_KEY>';
const apiURL = `https://api.openweathermap.org/data/2.5/weather?q=${searchbar.value}&units=metric&appid=${API_KEY}`;

document.querySelector('form').addEventListener('submit', (e) => {
  e.preventDefault();
 
  fetch(apiURL);
});

async function getWeather() { 
  const response = await fetch(apiURL);
  const data = await response.json();
  console.log(data);
}

如何解决400错误?

英文:

I am attempting to create a weather app as a way to learn how to code. I have designed the app myself and followed a tutorial for the JavaScript, but I encountered a 400 error when using the OpenWeather API. Here is my code:

const searchbar = document.querySelector(&#39;#search-bar&#39;);
const temperature = document.querySelector(&#39;.temperature&#39;);
const description = document.querySelector(&#39;.description&#39;);
const imageContainer = document.querySelector(&#39;.image-container&#39;);
const API_KEY = &#39;&lt;MY_API_KEY&gt;&#39;;
const apiURL = `https://api.openweathermap.org/data/2.5/weather?q=${searchbar.value}&amp;units=metric&amp;appid=${API_KEY}`;

document.querySelector(&#39;form&#39;).addEventListener(&#39;submit&#39;, (e) =&gt; {
  e.preventDefault();
 
  fetch(apiURL);
});

async function getWeather() { 
  const response = await fetch(apiURl);
  const data = await response.json();
  console.log(data);
}

What can I do to resolve the 400 error?

答案1

得分: 1

整合评论,您应该进行以下更改:

  • 每次重新评估URL
  • 使用 URL.searchParams
  • 在提交事件监听器中调用 getWeather()
document.querySelector('form').addEventListener('submit', (e) => {
  e.preventDefault();
  getWeather();
});

async function getWeather() { 
  const apiURL = new URL("https://api.openweathermap.org/data/2.5/weather");
  apiURL.searchParams.set("q", searchbar.value);
  apiURL.searchParams.set("units", "metric");
  apiURL.searchParams.set("appid", API_KEY);
  const response = await fetch(apiURL.toString());
  const data = await response.json();
  console.log(data);
}
英文:

Aggregating the comments, you should make the following changes:

  • re-evaluate the url everytime
  • use URL.searchParams
  • call getWeather() in submit event listener
document.querySelector(&#39;form&#39;).addEventListener(&#39;submit&#39;, (e) =&gt; {
  e.preventDefault();
  getWeather();
});

async function getWeather() { 
  const apiURL = new URL(&quot;https://api.openweathermap.org/data/2.5/weather&quot;);
  apiURL.searchParams.set(&quot;q&quot;,searchbar.value);
  apiURL.searchParams.set(&quot;units&quot;,&quot;metric&quot;);
  apiURL.searchParams.set(&quot;appid&quot;,API_KEY);
  const response = await fetch(apiURL.toString());
  const data = await response.json();
  console.log(data);
}

答案2

得分: 0

  1. apiURL 变量被移到事件监听器内部,因此在表单提交时,它获取 #search-bar 输入的当前值

  2. 在表单提交时调用 getWeather 函数。

  3. 使用 try-catch 处理在获取操作期间可能出现的任何错误。

英文:
  1. The apiURL variable is moved inside the event listener, so it takes the current value of the #search-bar input when the form is submitted.

  2. Call getWeather function when the form is submitted.

  3. Use try-catch to handle any potential errors during the fetch operation.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const searchbar = document.querySelector(&#39;#search-bar&#39;);
const API_KEY = &#39;&lt;MY_API_KEY&gt;&#39;;

document.querySelector(&#39;form&#39;).addEventListener(&#39;submit&#39;, (e) =&gt; {
  e.preventDefault();
  
  const apiURL = `https://api.openweathermap.org/data/2.5/weather?q=${searchbar.value}&amp;units=metric&amp;appid=${API_KEY}`;
  
  getWeather(apiURL);
});

async function getWeather(apiURL) {
  try {
    const response = await fetch(apiURL);
    const data = await response.json();
    console.log(data);
    
  } catch (error) {
    console.error(&#39;Error fetching weather data:&#39;, error);
  }
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月16日 11:00:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定