сбой сетевого запроса setTimeout $ argument_0 в реакции native? - PullRequest
0 голосов
/ 05 августа 2020

Я создаю собственное приложение для тестирования своего API, и оно размещено. В ПОСТМАНЕ работает нормально. Но когда я использую метод fetch для доступа к своему API, он выдает ошибку «сбой сетевого запроса». Я попытался получить метод с помощью этой ссылки «https://jsonplaceholder.typicode.com/todos/1», и он работает нормально. Как я могу исправить свою проблему?

Вы можете проверить мой API, используя «https://devapps.sansutech.com/EventApp/swagger/index.html» по этой ссылке. Я также пробовал шаги в « React native TypeError: сбой сетевого запроса с помощью fetch () » по этой ссылке. Но у меня ничего не работает. Пожалуйста, помогите мне решить эту проблему.

This is the error I got. error2

import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Text,
  TouchableHighlight,
  Alert
} from 'react-native';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      uname: 'admin',
      upassword: '123',
    };    
  }

  getDataFromAPI = () => {
    // Alert.alert("Hi there")
    const {uname} = this.state;
    const {upassword} = this.state;
    // console.warn(uname)
// https://jsonplaceholder.typicode.com/todos/1
    fetch('https://devapps.sansutech.com/EventApp/api/User/GetUser?username='+uname, {
      method: 'GET'
   })
   .then((response) => response.json())
   .then((responseJson) => {
      console.warn(responseJson);
   })
   .catch((error) => {
      console.error(error);
   });
  };
  postDataFromAPI = () => {
    // Alert.alert("Hi there")
    const {uname} = this.state;
    const {upassword} = this.state;
    // console.warn(uname)

      fetch('https://devapps.sansutech.com/EventApp/api/User/userLogin', {
          method: 'POST',
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            user_Name: uname,
            password: upassword,
          }),
        })
          .then(response => response.json())
          .then(responseJson => {
            console.warn(responseJson);
          })
          .catch(error => {
            console.error(error);
          });  
  };
  putDataFromAPI = () => {
    Alert.alert("Hi there")
  };
  deleteDataFromAPI = () => {
    Alert.alert("Hi there")
  };

  render() { 
    return ( 
      <View style={styles.container}>
        <Text style={styles.title}>Test API Methods</Text>
        
        <TouchableHighlight style={[styles.buttonContainer, styles.loginButton]} onPress={() => this.getDataFromAPI()}>
          <Text style={styles.buttonText}>Get</Text>
        </TouchableHighlight>
        <TouchableHighlight style={[styles.buttonContainer, styles.loginButton]} onPress={() => this.postDataFromAPI()}>
          <Text style={styles.buttonText}>Post</Text>
        </TouchableHighlight>
        <TouchableHighlight style={[styles.buttonContainer, styles.loginButton]} onPress={() => this.putDataFromAPI()}>
          <Text style={styles.buttonText}>Put</Text>
        </TouchableHighlight>
        <TouchableHighlight style={[styles.buttonContainer, styles.loginButton]} onPress={() => this.deleteDataFromAPI()}>
          <Text style={styles.buttonText}>Delete</Text>
        </TouchableHighlight>
      </View>
     );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#EEEEEE',
    alignItems: 'center',
    paddingTop:50,
  },
  title:{
    fontSize:24,
    textAlign: 'center',
    marginTop:22,
    marginBottom:40,
    color: "#5F6D7A"
  },
  description: {
    marginTop:20,
    textAlign: 'center',
    color: "#A9A9A9",
    fontSize:16,
    margin:40,
  },
  buttonContainer: {
    height:45,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom:20,
    width:250,
    borderRadius:30,
  },
  loginButton: {
    backgroundColor: "#3498db",
  },
  buttonText: {
    color: "#FFFFFF",
    fontSize:20,
  }
});
 
export default App;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...