[英]React.js - Can't fetch API
I am trying to build an API website for my portfolio using the NASA Image and Video Library API.我正在尝试使用 NASA 图像和视频库 API 为我的投资组合构建一个 API 网站。 When I type in the fetch in my browser ( https://images-api.nasa.gov/search?q=%22%22 ), the collection pops up.
当我在浏览器中输入 fetch ( https://images-api.nasa.gov/search?q=%22%22 ) 时,会弹出集合。 Yet when I try to fetch it in React.js, nothing shows!
然而,当我尝试在 React.js 中获取它时,什么也没显示!
I've tried axios, and trying different searches, using state and consts, and the fetch never gives me anything.我已经尝试过 axios,并尝试使用 state 和 consts 进行不同的搜索,但提取从未给我任何东西。
Here is my App code, please help if you can I'm so excited to make this website but I can't get past this bug:这是我的应用程序代码,如果可以,请提供帮助我很高兴制作这个网站,但我无法克服这个错误:
import React from 'react';
import './App.css';
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from "react-router-dom";
import Test from "./components/test";
import axios from "axios";
export default function App() {
const [data, setData] = React.useState([]);
async function getData() {
await fetch("https://images-api.nasa.gov/search?q=apollo")
.then(res => setData([res]));
}
return (
<Router>
<Switch>
<Route exact path="/">
<ul>
{data}
</ul>
{/* LANDING PAGE + Picture of the Day*/}
</Route>
<Route path="/browse">
{/* Most Popular Pictures */}
</Route>
<Route path="/mars-rover">
{/* Mars Rover */}
</Route>
</Switch>
</Router>
);
}
You will need to call your getData function like so您需要像这样调用您的 getData function
React.useEffect(() => {
getData();
},[]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.