【问题标题】:react native request with axios用 axios 响应本机请求
【发布时间】:2019-10-30 12:52:08
【问题描述】:

我正在尝试向我的 nodeJS 后端 API 发出 axios 请求。 我正在使用 Genymotion,请求设置如下:

async componentDidMount() {
            const response = await axios.get('http://10.0.3.2:3333/posts');

            console.log(response)
    }

我收到以下回复:

但我希望获得真实数据,例如在浏览器中发出 get 请求时:

所以我只遇到来自其他域的请求的问题

数据字符串:

这是主要的入口点控制器:

Routes.js
routes.get('/posts', postController.index);

postController.index

async index(req, res) {
        console.log(req)
        const post = await Post.find().sort('-createdAt')
        return res.json(post)
    }

请求没有到达 console.log

【问题讨论】:

  • 能否从后端添加返回数据的方法?
  • 我已经编辑了问题
  • 您确定您拨打了正确的电话const response = await axios.get('http://10.0.3.2:3333/posts');?在您的浏览器中,您会看到 http://localhost:3333/posts
  • 哦,您使用的是 Genymotion,只需确保主机正确,尝试在浏览器中访问 http://10.0.3.2:3333/posts 可能吗?
  • Genymotion 不使用本地主机进行连接

标签: node.js react-native asynchronous axios genymotion


【解决方案1】:

我已经使用自己的 IP 地址解决了这个问题,我复制了我的 IPV6 地址并将其用作地址来发出请求。

【讨论】:

    猜你喜欢
    • 2020-10-22
    • 2017-09-27
    • 2020-09-25
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2021-01-10
    相关资源
    最近更新 更多