如何在获取时修复 404
How to fix 404 while fetching
好的,我现在正在从 openweather API 开发一个简单的 API 项目并制作我自己的项目,但是当我尝试从 API 获取数据时卡住了我收到 404
错误
这是我的代码:
async function fetchData(cityName) {
const API_KEY = 'MY_API_KEY';
const fethRes = await fetch(
`api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}`,
{
mode: 'cors',
}
);
}
这是我得到的错误
GEThttp://127.0.0.1:5500/dist/api.openweathermap.org/data/2.5/weather?q=paris&appid={MY_API_KEY}
出于安全原因,我隐藏了我的 api 密钥,希望您能理解:)
已更新,我已经测试了代码,现在可以使用了
async function fetchData(cityName) {
const API_KEY = 'Your_API_KEY';
const fethRes = {
method: 'GET',
redirect: 'manual'
};
await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}`, fethRes)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}
好的,我现在正在从 openweather API 开发一个简单的 API 项目并制作我自己的项目,但是当我尝试从 API 获取数据时卡住了我收到 404
错误
这是我的代码:
async function fetchData(cityName) {
const API_KEY = 'MY_API_KEY';
const fethRes = await fetch(
`api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}`,
{
mode: 'cors',
}
);
}
这是我得到的错误
GEThttp://127.0.0.1:5500/dist/api.openweathermap.org/data/2.5/weather?q=paris&appid={MY_API_KEY}
出于安全原因,我隐藏了我的 api 密钥,希望您能理解:)
已更新,我已经测试了代码,现在可以使用了
async function fetchData(cityName) {
const API_KEY = 'Your_API_KEY';
const fethRes = {
method: 'GET',
redirect: 'manual'
};
await fetch(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}`, fethRes)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}