【发布时间】:2019-12-21 19:22:52
【问题描述】:
我正在尝试使用 React Native 中的“Axios”从我的 localhost 服务器上的 API 获取数据,但未从 API 获取数据并在模拟器。
这是我的代码:
import React, { Component } from 'react';
import { StyleSheet, ActivityIndicator, ListView, Text, View, Alert } from 'react-native';
import axios from 'axios';
export default class pageOne extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
}
}
async componentDidMount(){
try{
await axios.post('http://192.168.38.230/reactTest/list.php')
.then(response => {
console.log(response);
this.setState({isLoading: false, dataSource: response})})
.catch(err => console.log(err));
} catch(error){
console.log('Fetch Error:', error)
}
}
render() {
if (this.state.isLoading) {
return (
<View>
<ActivityIndicator />
</View>
);
}
return (
<ListView
dataSource={this.state.dataSource}
renderSeparator= {this.ListViewItemSeparator}
enableEmptySections = {true}
renderRow={(rowData) => <Text style={styles.rowViewContainer}
onPress={this.GetItem.bind(this, rowData.cl_name)} >{rowData.cl_name}</Text>}
/> );}}
这是api数据
[{"id":"1","cl_name":"test11"},{"id":"2","cl_name":"test12"},{"id":"3","cl_name":"test13"},{"id":"4","cl_name":"test14"}]
【问题讨论】:
-
您的 url 并没有真正指向您的本地服务器。您的网址中的“IP 地址”是什么?
-
我电脑的IP地址
标签: reactjs react-native axios