iOS设备Axios请求失败:Network Error/404问题排查求助
iOS真机/Expo Go中Axios请求失败:Network Error或404问题排查
在iOS设备上所有向API发起的请求均失败,报错为Network Error或Request failed with status code 404。
背景信息
- 应用基于React Native和Expo开发
- API采用Node.js + TypeScript构建,部署在个人VPS的Nginx服务器上,端口8080,未使用HTTPS
- Axios请求在Mac的iOS模拟器中可正常工作,但iOS真机、手机Expo Go中均失败
- 应用通过
eas build构建后用eas submit部署
目前无法定位失败的原因和位置。
已尝试的解决方案
方案1:升级Expo SDK
将Expo SDK从48版本升级到49版本,但出现其他各类问题且Axios错误仍存在,已降级回48版本(49版本仅发布3天)。
方案2:服务器端tcpdump排查
执行命令sudo tcpdump -i any port 8080检查服务器是否收到请求,结果显示错误发生时服务器未收到任何请求,说明问题出在请求到达服务器之前。
方案3:集成Sentry
安装Sentry以获取更详细的错误栈信息,但得到的信息无有效帮助。
方案4:GET请求添加data参数
尝试在GET请求中添加data: {}或data: null,未解决问题,后续清理服务器时发现该配置反而阻碍请求正常工作。
方案5:分析Axios请求及错误栈
请求示例代码
let targetEndpoint = 'http://example.org:8080/my-endpoint'; const config = getConfig(); // 获取请求头 try { const response = await axios.get(targetEndpoint, config); return response.data.data; } catch (error) { console.error('Error getting data: ', error); // <-- 错误触发位置 throw error; }
错误栈示例1(404错误)
{ "message": "Request failed with status code 404", "name": "AxiosError", "stack": "AxiosError: Request failed with status code 404\n at settle (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:140101:37)\n at onloadend (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:139997:29)\n at call (native)\n at dispatchEvent (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:31686:31)\n at setReadyState (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:30432:29)\n at __didCompleteResponse (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:30234:29)\n at apply (native)\n at anonymous (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:30359:52)\n at apply (native)\n at emit (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:2236:40)\n at apply (native)\n at __callFunction (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:2805:36)\n at anonymous (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:2566:31)\n at __guard (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:2756:15)\n at callFunctionReturnFlushedQueue (http://192.168.0.53:19000/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false:2565:21)", "config": { "transitional": { "silentJSONParsing": true, "forcedJSONParsing": true, "clarifyTimeoutError": false }, "adapter": [ "xhr", "http" ], "transformRequest": [ null ], "transformResponse": [ null ], "timeout": 0, "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN", "maxContentLength": -1, "maxBodyLength": -1, "env": {}, "headers": { "Accept": "application/json, text/plain, */*" }, "method": "get", "url": "http://example.org:8080/my-endpoint" }, "code": "ERR_BAD_REQUEST", "status": 404 }
错误栈示例2(Network Error)
{ "message": "Network Error", "name": "AxiosError", "stack": "AxiosError: Network Error\n at handleError (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:173910:39)\n at call (native)\n at dispatchEvent (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:42587:31)\n at setReadyState (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:40560:33)\n at __didCompleteResponse (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:40346:29)\n at apply (native)\n at anonymous (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:40488:52)\n at apply (native)\n at emit (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:3006:40)\n at apply (native)\n at __callFunction (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:3711:36)\n at anonymous (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:3431:31)\n at __guard (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:3648:15)\n at callFunctionReturnFlushedQueue (http://192.168.0.53:8081/node_modules/expo/AppEntry.bundle//&platform=ios&dev=true&hot=false&lazy=true:3430:21)", "config": { "transitional": { "silentJSONParsing": true, "forcedJSONParsing": true, "clarifyTimeoutError": false }, "adapter": [ "xhr", "http" ], "transformRequest": [ null ], "transformResponse": [ null ], "timeout": 0, "xsrfCookieName": "XSRF-TOKEN", "xsrfHeaderName": "X-XSRF-TOKEN", "maxContentLength": -1, "maxBodyLength": -1, "env": {}, "headers": { "Accept": "application/json", "content-type": "application/json" }, "data": "{}", "method": "get", "url": "http://example.org:8080/my-endpoint" }, "code": "ERR_NETWORK", "status": null }
内容的提问来源于stack exchange,提问作者Zeth
相关产品推荐
相关产品推荐

