首页 > 解决方案 > 使用 react-native 获取网络请求失败

问题描述

我是一个 fetch 的初学者,并试图从 JSON-server 获取一些数据到我的 react native 应用程序,但它总是给我同样的错误......“网络请求失败”我试图使用 Ip 而不是 localhost,如“ http:/ /192.168.xx:3000 " 但没有任何改变,我的博览会是 192.168.xx:19000。我忘了做什么?为什么这个错误总是发生?

export const fetchDishes = () => (dispatch) => {
dispatch(dishesLoading());

return fetch("http://192.168.1.5:3000/dishes")
.then(
  (response) => {
    if (response.ok) {
      return response;
    } else {
      var error = new Error(
        "Error " + response.status + ": " + response.statusText
      );
      error.respone = response;
      throw error;
    }
  },
  (error) => {
    var errmss = new Error(error.message);
    throw errmss;
  }
)
.then((respone) => respone.json())
.then((dishes) => dispatch(addDishes(dishes)))
.catch((error) => dispatch(dishesFailed(error.message)));
};

我的世博画面

标签: react-nativefetch

解决方案


推荐阅读