微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

类型错误:Android 模拟器中的网络请求在 React Native 中失败

如何解决类型错误:Android 模拟器中的网络请求在 React Native 中失败

我们尝试在 React Native 功能组件中加载页面调用 API。我们使用安卓模拟器

 useEffect(() => {
    //GET request
    fetch('https://jsonplaceholder.typicode.com/posts/1',{
      method: 'GET',//Request Type
    })
      .then((response) => response.json())
      //If response is in json then in success
      .then((responseJson) => {
        //Success
        alert(JSON.stringify(responseJson));
        console.log(responseJson);
      })
      //If response is not in json then in error
      .catch((error) => {
        //Error
        alert(JSON.stringify(error));
        console.error(error);
      });
  },[])

但我们收到[TypeError:网络请求失败]。请告诉我们我们做错了什么

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。