【问题标题】:React native Expo TypeError: Network request failedReact native Expo TypeError:网络请求失败
【发布时间】:2018-12-05 15:20:28
【问题描述】:

我正在尝试从由 ASP.NET Web API 应用程序生成的本地 API 获取数据。这个本地 API 在通过 Postman 程序测试时工作正常,但不适用于我的 react native 项目。我搜索了这个错误,我发现我应该用我的机器 IPv4 地址替换“localhost”,我这样做了,但我仍然得到同样的错误。我在装有 Android 7.0 NRD90M 的手机上使用 Expo 运行我的项目 这是我的代码:-

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;

【问题讨论】:

    标签: react-native expo


    【解决方案1】:

    我遇到了同样的问题并使用 type: 'multipart/form-data':

    修复了它
    var media = {uri: photo.uri ,type: 'multipart/form-data', name: filename, };
    

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多