在我的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>
);
}
}
答案 0 :(得分:0)
尝试先从浏览器访问api
http://localhost:3000/photos // if this works follow below
只说:
axios.get('/photos')
.then(response=>console.log(response));
return (
<View>
<Text> hello </Text>
</View>
);