【问题标题】:axios get request for localhost:3000/photos not returning anything?axios 获取对 localhost:3000/photos 的请求没有返回任何内容?
【发布时间】:2019-07-11 08:36:15
【问题描述】:

在我的 react native 项目中,我正在尝试使用 axios 从 GET 请求中获取数据。

我知道我需要将 axios.get 中的“localhost”更改为我机器的 IP,即使这样做后我也没有收到我的 console.log 语句的响应。

axios 代码:

    import React, { Component } from 'react';
import { View, Text } from 'react-native';
import axios from 'axios';
import PhotoSection from './photo-section';
//functional is just returning some data - static
//smart component for dynamic class component
export default class PhotoFeed extends Component {
  render() {
    axios.get('http://192.168.1.208:3000/photos')
    .then(response=>console.log(response));
      return (
      <View>
        <Text> hello </Text>
      </View>
    );
  }
}

【问题讨论】:

  • 在 .then 语句之后使用 .catch(err=&gt;console.log(err)); 这样您就可以看到错误是什么。

标签: javascript reactjs get axios expo


【解决方案1】:

尝试先从浏览器点击 api

http://localhost:3000/photos // if this works follow below

简单地说:

axios.get('/photos')
.then(response=>console.log(response));
  return (
  <View>
    <Text> hello </Text>
  </View>
);

【讨论】:

  • 我收到一个承诺未处理的拒绝并出现网络错误
  • 什么错误。要获取数据,您是否在任何不同的端口上运行任何后端?
猜你喜欢
  • 1970-01-01
  • 2021-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-18
  • 2013-05-14
  • 2020-08-16
  • 2013-12-28
相关资源
最近更新 更多