React native 反应本机Expo类型错误:网络请求失败
我正在尝试从ASP.NET Web API应用程序生成的本地API获取数据。当Postman程序对这个本地API进行测试时,它工作得很好,但它不适用于我的react原生项目。我搜索了这个错误,发现我应该用我的机器IPv4地址替换“localhost”,我这样做了,但仍然得到相同的错误。我在安卓7.0版NRD90M手机上使用Expo运行我的项目 这是我的密码:-React native 反应本机Expo类型错误:网络请求失败,react-native,expo,React Native,Expo,我正在尝试从ASP.NET Web API应用程序生成的本地API获取数据。当Postman程序对这个本地API进行测试时,它工作得很好,但它不适用于我的react原生项目。我搜索了这个错误,发现我应该用我的机器IPv4地址替换“localhost”,我这样做了,但仍然得到相同的错误。我在安卓7.0版NRD90M手机上使用Expo运行我的项目 这是我的密码:- import React, { Component } from 'react'; import { StyleSheet, Text,
import React, { Component } from 'react';
import { StyleSheet, Text, View, Image} from 'react-native';
import { Icon, Button, Container, Header, Content, Left} from 'native-base';
const EXAMPLE_ENDPOINT = "http://192.168.1.5:57975/api/values/5";
class TestAPI extends Component {
constructor(props) {
super(props)
this.state = {
value: null
}
}
componentDidMount() {
this._fetchExampleAsync();
}
_fetchExampleAsync = async () => {
try {
let response = await fetch(EXAMPLE_ENDPOINT);
let result = await response.json();
this.setState({value});
} catch(e) {
this.setState({value: e});
}
};
render() {
console.log("Value: ", this.state.value);
return (
<Container>
<Content contentContainerStyle={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text>Test fetch API</Text>
<Text>values: {JSON.stringify(this.state.value)}</Text>
</Content>
</Container>
);
}
}
export default TestAPI;
import React,{Component}来自'React';
从“react native”导入{样式表、文本、视图、图像};
从“本机基础”导入{图标、按钮、容器、标题、内容、左侧};
常量示例_端点=”http://192.168.1.5:57975/api/values/5";
类TestAPI扩展组件{
建造师(道具){
超级(道具)
此.state={
值:null
}
}
componentDidMount(){
这是。_fetchExampleAncy();
}
_FetchExampleAncy=async()=>{
试一试{
let response=wait fetch(示例_端点);
让result=wait response.json();
this.setState({value});
}捕获(e){
this.setState({value:e});
}
};
render(){
log(“值:”,this.state.Value);
返回(
测试获取API
值:{JSON.stringify(this.state.value)}
);
}
}
导出默认的TestAPI;
我也遇到了同样的问题,并使用以下类型修复了它:'multipart/form data':
var media = {uri: photo.uri ,type: 'multipart/form-data', name: filename, };