稳定、快速、免费的 API 接口服务
获取用户的IP地理位置及天气状况
接口地址: https://api.fenx.top/api/getWeather
返回格式: 请看返回示例
请求方式: GET
请求示例: https://api.fenx.top/api/getWeather?type=json
请求参数说明:
名称 | 必填 | 类型 | 说明 |
---|---|---|---|
json | 选填 | string | 返回JSON格式的数据(默认值)。 |
text | 选填 | string | 返回文本格式的数据。 |
返回参数说明:
名称 | 类型 | 说明 |
---|---|---|
json | string | 请看返回示例 |
text | string | 来自[城市]的朋友,今天是[当前日期],[星期几],天气[天气状况],温度[温度]度,湿度[湿度]%。祝您有美好的一天! |
返回示例:
请求参数设置:
参数名称 | 参数值 |
---|---|
错误码格式说明:
名称 | 类型 | 说明 |
---|
代码示例:
<p id="weather-info">正在加载天气信息...</p>
<script>
// 获取天气信息的函数
async function getWeather() {
const apiUrl = "https://api.fenx.top/api/getWeather?type=text";
try {
const response = await fetch(apiUrl);
if (response.ok) {
const data = await response.text();
document.getElementById("weather-info").innerText = data;
} else {
document.getElementById("weather-info").innerText = "无法获取天气信息,请稍后再试。";
}
} catch (error) {
console.error("获取天气信息时出错:", error);
document.getElementById("weather-info").innerText = "获取天气信息时出错,请检查网络连接。";
}
}
// 页面加载完成后调用获取天气信息的函数
window.onload = getWeather;
</script>