首页 > 解决方案 > 我需要一些关于 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)

标签: javascriptaxios

解决方案


您必须通过 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));

推荐阅读