导出weather.js函数至index.js时出现net::ERR_ABORTED 404错误求助
解决net::ERR_ABORTED 404 (Not Found)错误
1. 修复API URL中的转义字符
你的weather.js里的API URL使用了&(HTML转义的&符号),但在JS字符串中应该直接用&,否则会导致URL参数解析错误,API无法识别请求路径,返回404。
修改weather.js中的URL:
export function getWeather(lat, lon, timezone) { return axios.get( 'https://api.open-meteo.com/v1/forecast?hourly=temperature_2m,apparent_temperature,precipitation,weathercode,windspeed_10m&daily=weathercode,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,precipitation_sum¤t_weather=true&precipitation_unit=inch&timeformat=unixtime', { params: { latitude: lat, longitude: lon, timezone, }, } ); }
2. 确保axios已正确引入
因为你使用了ES模块,需要确保axios能被正确加载:
- 若通过npm管理项目,先安装axios:
npm install axios,然后在weather.js顶部添加导入:import axios from 'axios'; - 若为纯前端静态项目,在HTML中引入axios的CDN脚本(放在你的
index.js引入之前):
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> <script type="module" defer src="./index.js"></script>
3. 验证请求URL是否正确
修改后可在浏览器开发者工具的Network面板查看实际发送的请求URL,确认参数(latitude、longitude、timezone)是否正确拼接,API地址是否为https://api.open-meteo.com/v1/forecast。
4. 检查文件路径是否正确
确保weather.js和index.js在同一目录下,import {getWeather} from './weather';的路径正确,避免因文件找不到导致的间接异常。
内容的提问来源于stack exchange,提问作者Neilson Programmer
相关产品推荐
相关产品推荐

