我的React不会从移动移动机上的API呈现数据,而是在PC上渲染

发布于 2025-02-08 06:52:48 字数 1878 浏览 2 评论 0原文

我正在研究这个项目,该项目从我的RESTFUL API中获取数据并使用它来更新状态。它可以在PC/笔记本电脑上完美运行,但是当我在移动设备上访问它时,它不会显示API中的任何内容,它仅显示页面和静态文件, 在下面的代码中,我使用地图从帖子状态创建元素

,或者我做错了


import '../css/pages/Home.css';
import {Link} from "react-router-dom";
import {useEffect, useState} from "react";
import Posts from "../components/Posts";

export const Home = () => {
    const [posts, setPosts] = useState([])


    async function fetch_post(link) {
        const res = await fetch(link)
        const data = await res.json()
        setPosts([...data]);

    }

    useEffect(() => {
        fetch_post("http://127.0.0.1:8000/")
    }, [])
    return (
        <>
            <section className={'intro'}>
                <div className={'intro-con'}>
                    <h1 className="intro-header">Welcome here</h1>
                    <p className="intro-msg">

                        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur aut
                        culpa cum distinctio excepturi, fuga hic id praesentium reiciendis similique veniam voluptate
                        voluptates? Accusamus ad aliquam consequuntur cupiditate dolor, dolorem dolores earum eius
                        eligendi fuga illum ipsam molestiae, nulla omnis, praesentium quae quia quibusdam repellat totam
                        voluptate? Ex, molestias, voluptatem?</p>
                    <Link to={'about/'} className={'btn'} style={{width: "max-content"}}>Learn More</Link>
                </div>
            </section>
            <section className="posts-feed contain">

                {posts.map(post => {
                    return (
                        <Posts post={post}/>
                    );
                })}

            </section>
        </>

    )}

i am working on this project that get data from my restful api and use it to update the state . It works perfectly on pc/laptop , but when i access it on mobile it doesn't show anything from the api, it only shows the page and and static file,
in the code below i use map to create the elements from the post state

is there a work around or am i doing it wrong


import '../css/pages/Home.css';
import {Link} from "react-router-dom";
import {useEffect, useState} from "react";
import Posts from "../components/Posts";

export const Home = () => {
    const [posts, setPosts] = useState([])


    async function fetch_post(link) {
        const res = await fetch(link)
        const data = await res.json()
        setPosts([...data]);

    }

    useEffect(() => {
        fetch_post("http://127.0.0.1:8000/")
    }, [])
    return (
        <>
            <section className={'intro'}>
                <div className={'intro-con'}>
                    <h1 className="intro-header">Welcome here</h1>
                    <p className="intro-msg">

                        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur aut
                        culpa cum distinctio excepturi, fuga hic id praesentium reiciendis similique veniam voluptate
                        voluptates? Accusamus ad aliquam consequuntur cupiditate dolor, dolorem dolores earum eius
                        eligendi fuga illum ipsam molestiae, nulla omnis, praesentium quae quia quibusdam repellat totam
                        voluptate? Ex, molestias, voluptatem?</p>
                    <Link to={'about/'} className={'btn'} style={{width: "max-content"}}>Learn More</Link>
                </div>
            </section>
            <section className="posts-feed contain">

                {posts.map(post => {
                    return (
                        <Posts post={post}/>
                    );
                })}

            </section>
        </>

    )}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2025-02-15 06:52:48

这是因为您正在使用localhost地址获取数据IE http://127.0.0.1:8000/
后端在您的PC/笔记本电脑上运行,因此它可以在此处工作,但在移动设备上却不能。为了使其在移动设备上可见,请使用您的LAN地址而不是Localhost。
在Windows OS上,您可以打开CMD并通过此命令检查:
IP配置
在MacOS上,您可以启动终端并检查:
ifconfig

它将像:192.168.1.12(只是一个示例)。

因此,在您的使用效果挂钩中,您可以使用:

useEffect(() => {
        fetch_post("http://192.168.1.12:8000/")
    }, [])

那么您也可以在移动设备上查看数据。

This is because you are using localhost address to fetch data i.e. http://127.0.0.1:8000/.
The backend is running on your pc/laptop so it would work there, but not on mobile. To make it visible on the mobile use your LAN address instead of localhost.
On Windows OS, you can open up cmd and check by this command:
ip config
On MacOS, you can fire up your terminal and check by:
ifconfig

It would be something like: 192.168.1.12 (just an example).

So inside your useEffect hook, you can use:

useEffect(() => {
        fetch_post("http://192.168.1.12:8000/")
    }, [])

Then you will be able to view your data on mobile as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文