请求协助排查Axios网络错误:Chat模块获取联系人失败
- 获取联系人失败,报错信息:
Error fetching contacts:, [AxiosError: Network Error]
at .expo\screens\Chat.js:null in fetchData
at node_modules@babel\runtime\helpers\asyncToGenerator.js:null in asyncGeneratorStep
at node_modules@babel\runtime\helpers\asyncToGenerator.js:null in Promise$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:null in queueReactNativeMicrotask
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:null in _callTimer
... 8 more stack frames from framework internals
- API路由
http://192.168.0.126:8082可正常获取信息,但Chat模块调用异常 - Postman中用HTTPS请求该路由返回
cannot get
修正请求协议
你的API使用HTTP协议,Postman用HTTPS请求失败说明Chat模块可能误用了HTTPS协议。打开.expo/screens/Chat.js的fetchData函数,确认请求URL前缀是http://而非https://。排查网络连通性
- 确认运行Expo的设备和API服务器在同一局域网,测试设备能否ping通
192.168.0.126 - 检查API服务器防火墙设置,确保8082端口对外开放
- iOS模拟器可尝试将地址替换为
localhost;Android模拟器替换为10.0.2.2(映射本地主机)
- 确认运行Expo的设备和API服务器在同一局域网,测试设备能否ping通
细化Axios错误排查
在fetchData中添加错误详情打印,定位具体问题:try { const response = await axios.get('http://192.168.0.126:8082/your-contacts-endpoint'); // 处理响应逻辑 } catch (error) { console.error('完整错误:', error); console.error('错误代码:', error.code); console.error('响应数据:', error.response?.data); }检查API端点路径
确认Chat模块调用的是正确的联系人接口路径(比如/api/contacts),Postman请求时需带上完整路径,仅请求根地址可能返回404导致cannot get。
内容的提问来源于stack exchange,提问作者Jun

