javascript - 我需要一些关于 JS 中 Axios Lab 的帮助
问题描述
我的问题是:我需要使用axios
库从 API 获取一些数据,但在某些情况下,它只是不起作用。
这是一个工作示例:
const RAPIDAPI_API_URL = 'https://cbsservis.tkgm.gov.tr/megsiswebapi.v3/api/parsel/40.89253647288918/29.236472547054294/';
const RAPIDAPI_REQUEST_HEADERS = {
'Content-Type': 'application/json'
};
axios.get(RAPIDAPI_API_URL,{ headers: RAPIDAPI_REQUEST_HEADERS })
.then(response => {
const data = response;
console.log(data);
})
.catch(error => console.error('On create error', error));
但是当 的值RAPIDAPI_API_URL
更改为“ http://jsonplaceholder.typicode.com/users ”时,会抛出以下错误:
Network Error
at e.exports (https://unpkg.com/axios@0.19.2/dist/axios.min.js:2:9633)
at XMLHttpRequest.l.onerror (https://unpkg.com/axios@0.19.2/dist/axios.min.js:2:8398)
解决方案
您必须通过 https 而不是 http 加载请求。在这里找到样品
const RAPIDAPI_API_URL = 'https://jsonplaceholder.typicode.com/users';
const RAPIDAPI_REQUEST_HEADERS = {
'Content-Type': 'application/json'
};
axios.get(RAPIDAPI_API_URL,{ headers: RAPIDAPI_REQUEST_HEADERS })
.then(response => {
const data = response;
console.log(data);
})
.catch(error => console.error('On create error', error));
推荐阅读
- azure - ARM Microsoft.Web/sites "kind" 可能的值
- c++ - 在构造函数中分配内存失败如何冒泡
- css - 无法垂直对齐 UL 内的按钮
- swift - 如何在 Firebase 数据库中解压缩多级嵌套 JSON
- c++ - C++ socket.io 客户端通过 Tor 服务
- python - Plotly:重新标记滑块上的动画刻度线
- c# - 具有继承类的实体框架核心模型生成器
- cassandra - Cassandra 是否会丢弃失败的写入记录或在读取修复期间传播它?
- android - 在 Android 上使用 Viewpager2 的底页
- ruby-on-rails - 加载rails控制台时如何修复'mysql2'错误?