为什么我总是在 React Native 中收到“网络请求失败”错误?

时间:2021-04-03 17:42:28

标签: node.js react-native express networking fetch

目前我正在做一个个人项目。我创建了一个 express js api 和简单的 React Native 应用程序。当尝试获取一个简单的 json 表单时,我的 api 应用程序失败。我不知道是什么问题?

App.js(反应原生应用)

import React, { useState } from 'react';
import { Button, StyleSheet, Text, View } from 'react-native';
import globalStyles from "./styles/globalStyles";

export default function App() {

  const [na, setNa] = useState("Başlık");

  const getTestAPI = async () => {
    try {
      let response = await fetch(
        '192.168.1.23:3000/api/getTest', {
          method: 'GET',
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json'
          },
        }
      );
      let json = await response.json();
      return json;
    } catch (error) {
       console.error(error);
    }
  };

  const get = () => {
    console.log(getTestAPI.title);
    console.log(getTestAPI());
    setNa(getTestAPI.title);
  };

  return (
    <View style={globalStyles.container}>
      <Text onPress={get} >Değerler : {na}</Text>
      <Text>cREATED BY FANTA</Text>
    </View>
  );
}

server.js (EXPRESS JS API)

    // localhost:3000/api/get   
router.get("/getTest", (req, res) => {
    res.json({"title":"Welcome to the test api get route"});
});

错误

ERROR IMG

0 个答案:

没有答案