简体   繁体   English

axios获取对localhost:3000 / photos的请求,但不返回任何内容?

[英]axios get request for localhost:3000/photos not returning anything?

In my react native project i am trying to get data from a GET request using axios. 在我的本机反应项目中,我试图使用axios从GET请求中获取数据。

I know i need to change the 'localhost' in axios.get to the IP of my machine, even after doing this im not getting a response for my console.log statement. 我知道我需要将axios.get中的'localhost'更改为我的计算机的IP,即使这样做之后,我也没有收到我的console.log语句的响应。

axios code: 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>
    );
  }
}

Try hitting api from browser first 尝试先从浏览器访问api

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

Just put: 只需输入:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM