【问题标题】:I encountered this error: Unhandled Rejection-Failed to fetch我遇到了这个错误:Unhandled Rejection-Failed to fetch
【发布时间】:2023-02-15 23:54:00
【问题描述】:

我正在为投资组合目的创建一个类似 netflix 的应用程序,但我遇到了这个错误:未处理的拒绝(TypeError):无法获取。 在我的这部分代码中似乎存在问题:

const [movies, setMovies] = useState([]);

useEffect(() => {
    fetch("https://movies-couch-api-herra17.vercel.app/movies") 
    .then((response) => response.json())
    .then((data) => {
        console.log(data); 

恰好在 .then((响应... 有人可以分享线索,或者可能会知道我还需要审查什么。 我有一个可用的 API。客户端组件是用 React 构建的。 我有以下依赖项:prop-types、react、react-dom、parcel/transformer-sass。

此外,这是我的 index.jsx 的样子:

import { createRoot} from 'react-dom/client';
import { MainView } from './components/main-view/main-view';
// Import statement to indicating bundle is needed import "./index.scss";
// Main component (will eventually use all others)
const MoviescouchApplication = () => { return <MainView />; }; // Find the root of your app const container = document.querySelector("#root"); const root = createRoot(container);
// Tells React to render your app in the root DOM element 
root.render(<MoviescouchApplication/>);

【问题讨论】:

    标签: reactjs api parcel


    【解决方案1】:

    这是由于 https://movies-couch-api-herra17.vercel.app/movies 的 API 阻止了跨域来源访问它。如果您尝试在控制台中获取该 API,您应该能够看到相同的错误。

    【讨论】:

    • 谢谢@khangnd
    • 很高兴能帮助你。如果它确实回答了您的问题,请不要忘记将其标记为答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 2018-06-19
    • 2023-02-15
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多