javascript - 在 Codesandbox 中获取问题
问题描述
我正在代码沙箱中编写我的代码,并且非常简单地尝试获取天气 API。我已经在代码沙箱中完成了许多 API 调用,它一直都很好,除了这个。
我在控制台中运行它,它也可以很好地返回数据。我真的无法理解是什么原因造成的?如果有人可以看一看并提供帮助,那将非常感激。这是代码:
import React, { useState, useEffect } from "react";
const WeekContainer = () => {
const [data, setData] = useState([]);
async function getData() {
const weatherURL = `http://api.openweathermap.org/data/2.5/forecast?zip=11102&units=imperial&APPID=dcadf332823cddfb979926a1414274e8`;
const result = await fetch(weatherURL);
const a = await result.json();
console.log(a)
setData(a)
}
useEffect(() => {
getData();
}, []);
return <div>weekcontainer</div>;
};
export default WeekContainer;
谢谢你的时间
解决方案
您的代码给出以下错误 -
The page at 'https://codesandbox.io/' was loaded over HTTPS, but requested an insecure resource 'http://api.openweathermap.org/data/2.5/forecast?zip=11102&units=imperial&APPID=dcadf332823cddfb979926a1414274e8'. This request has been blocked; the content must be served over HTTPS.
这是因为我们无法从 HTTPS 页面访问 HTTP 内容。检查这个 - HTTP Ajax Request via HTTPS Page。
您正在尝试从在 HTTPS 上运行并通过 http 调用 api.openweathermap.org 的代码沙箱中获取数据。将 API 请求更改为 https://api.openweathermap.org/data/2.5/forecast?zip=11102&units=imperial&APPID=dcadf332823cddfb979926a1414274e8
,您应该能够获取数据。
推荐阅读
- telegram - Telegram Bot 加入新用户以踢白名单并保留私人组的成员?
- angular - 离子角度应用程序中的Router.navigate问题
- autoit - Autoit - “可能在声明之前使用”和“未声明的全局变量”编译错误
- swift - 使用 Amadeus API 在 SWIFT 中出现“tooManyRequests”错误
- ios - Flutter iOS 模块无法访问新的 Swift 文件并且打印不工作
- c++ - C++ 类上下文中的引用和指针
- app-store-connect - 如何在 App Store Connect 中提交最新版本?
- javascript - 解析时 JSON 输入意外结束
- spring-boot - Java 库或公钥/私钥消息加密
- html - 如何在两个地方共享同一个div