在尝试在JSFIDDLE中获取资源时的NetworkError
我正在为网页制作自动完成功能,用户可以通过输入城市/机场名称的首字母来查找有关城市及其机场的信息。我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您检查浏览器控制台,您会发现以下内容:
混合内容:“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.