在尝试在JSFIDDLE中获取资源时的NetworkError

发布于 2025-01-19 06:19:30 字数 487 浏览 2 评论 0原文

我正在为网页制作自动完成功能,用户可以通过输入城市/机场名称的首字母来查找有关城市及其机场的信息。我使用 javascript fetch 从 API 接收所有活动机场:

let airports; 

fetch("http://api.travelpayouts.com/data/ru/airports.json")
    .then(data => data.json())
    .then(data => airports = JSON.parse(data))
    .catch(error => console.log(error.message));

它在本地工作,但在 jsfiddle.net 的情况下,我收到错误:

尝试获取资源时出现网络错误。

我用 google 搜索了该问题,但只能找到这种行为的原因并且没有解决方案,所以我最终只是将响应正文硬编码到小提琴中。有什么方法可以正确修复吗?

I'm making an autocomplete function for a webpage where users can find information about cities and their airports by typing the first letters of the city/airport name. I use javascript fetch for receiving all the active airports from the API:

let airports; 

fetch("http://api.travelpayouts.com/data/ru/airports.json")
    .then(data => data.json())
    .then(data => airports = JSON.parse(data))
    .catch(error => console.log(error.message));

It works locally but in case of jsfiddle.net i receive an error:

NetworkError when attempting to fetch resource.

I googled the problem but could only find the reason of such behavior and no solution so I end up with just hardcoding the response body into the fiddle. Any ways to fix it properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2025-01-26 06:19:30

如果您检查浏览器控制台,您会发现以下内容:

混合内容:“https://jsfiddle.net/”页面是通过 HTTPS 加载的,但请求了不安全的资源“http://api.travelpayouts.com/data” /ru/airports.json'。该请求已被阻止;内容必须通过 HTTPS 提供。

您无法从通过 HTTPS 加载的站点向不安全的资源 (http) 发出请求。

开发过程中的一种解决方法是通过允许不安全的内容来更改浏览器上的站点设置。

If you check the browser console, you will find this:

Mixed Content: The page at 'https://jsfiddle.net/' was loaded over HTTPS, but requested an insecure resource 'http://api.travelpayouts.com/data/ru/airports.json'. This request has been blocked; the content must be served over HTTPS.

You can't make a request from a site loaded over HTTPS to an insecure resource(http).

One work around while development would be to change the site settings on your browser by allowing insecure content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文