【发布时间】:2019-01-15 07:55:43
【问题描述】:
我正在尝试使用 axios 从 API 获取数据。
这是我发出获取请求的代码:
export const loadBloodGroups = () => {
return (dispatch) => {
dispatch({ type: LOADING_BLOOD });
const url = `http://www.example.org/api/bloodgroup/getusersbloodgroup`;
axios.get(url)
.then(response => loadingSuccess(dispatch, response))
.catch(error => loadingFail(dispatch, error));
};
};
const loadingFail = (dispatch, error) => {
console.log(error);
dispatch({
type: LOADING_BLOOD_FAIL,
payload: error
});
};
const loadingSuccess = (dispatch, response) => {
dispatch({
type: LOADING_BLOOD_SUCCESS,
payload: response
});
};
http 的 info.plist 设置:
在安卓模拟器上可以正常使用,但是IOS模拟器不行。
在我的浏览器调试控制台中显示:
它显示的 Mac 终端:
谁能告诉我,我在哪里做错了?还是我需要为 IOS 做任何其他配置?
我的平台:
- 操作系统:Mac 10.13
- 节点:10.7
- npm: 6.3.0
- 反应:16.4.1
- 反应原生:0.55
【问题讨论】:
-
尝试使用控制台检查错误,将
.catch(error => loadingFail(dispatch, error));更改为.catch(error => { console.log(error); loadingFail(dispatch, error) });请粘贴控制台消息的输出。 -
您可以尝试删除 catch() 部分并在 then() 部分中添加错误处理吗?就像这样 .then(response=> {}, error=>{})。这不会解决您的问题,但它可能会帮助您调试它。看看这条推文,了解我为什么建议这个twitter.com/dan_abramov/status/770914221638942720?lang=en
-
我测试了你建议的两种方式,但我得到了相同的Network error
-
@Arif 你找到解决方案了吗?我也有同样的问题。我可以从模拟器的 Safari 访问相同的 http url,但不能从我的应用程序访问。
-
@Yossi 而不是更改 http 的配置。我做了我的服务https。现在它工作正常。
标签: ios react-native ios-simulator axios