You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Expo应用中Axios无法发送本地HTTP请求排查

React Native Expo + .NET 后端:添加POST注册请求后所有Axios请求超时失效

我正在开发React Native Expo应用,对接运行稳定的.NET后端。此前移动端通过Axios发起的本地HTTP GET请求完全正常,请求在useEffect中调用,代码如下:

useEffect(() => {
    async function getAll() {
        const response = await axios({
            method: 'GET',
            url: `${baseUrl}/ItemName`,
            headers: {
                authorization: `Bearer ${authToken}`,
                'content-type': 'application/x-www-form-urlencoded'
            }
        }).catch(err => console.log(err));

        if(response?.data) {
            setItemName(response.data);
        }
    }

    getAll();
}, []);

为了替代后端Swagger手动复制令牌的方式,我新增了注册页面,表单功能正常,但添加POST注册请求后,所有请求(包括之前正常的GET)均失效,返回Axios超时错误,且后端未收到任何请求(后端控制器日志仅在Postman/Swagger请求时触发)。POST请求代码如下:

const postNewUser = async (values) => {
    try {
        const postObject = {
            method: 'POST',
            url: `${baseUrl}/User/Register`,
            data: {
                firstName: values.firstName,
                lastName: values.lastName,
                username: values.username,
                password: values.password,
                email: values.email,
                phoneNumber: values.phone,
                birthDate: moment(values.birthDate).toISOString()
            },
            headers: {
                'content-type': 'application/json'
            }
        };

        const response = await axios(postObject);

        if(response) {
            navigation.navigate('Login', {
                desiredUsername: values.username
            });
        }
    } catch(err) {
        console.log('Error: ', err);
    }
}

已进行的排查操作:

  • 更新Node.js至最新版本
  • 更换多个不同版本的Axios
  • 尝试使用不同的baseUrl:localhost、10.0.2.2、本地IPv4地址
  • 测试发现localhost和本地IP请求返回较快,10.0.2.2请求需等待30秒,但所有请求最终均返回相同的超时错误

完整Axios超时错误日志:

Stack: AxiosError@http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:114554:29
    handleTimeout@http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:115572:39
    dispatchEvent@http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:30065:31
    setReadyState@http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:28693:33
    __didCompleteResponse@http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:28499:29
    emit@http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:2310:40
    __callFunction@http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:21321:36
    @http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:21071:31
    __guard@http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:21269:15
    callFunctionReturnFlushedQueue@http://personalIP:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=ios&dev=true&hot=false:21070:21
    callFunctionReturnFlushedQueue@[native code]

Message: timeout exceeded
Name: AxiosError
Code: ECONNABORTED
Config: {
    "adapter": [
        "xhr",
        "http"
    ],
    "data": "{\"firstName\":\"Test\",\"lastName\":\"Test\",\"username\":\"test\",\"password\":\"P@ssw0rd\",\"email\":\"email@example.com\",\"phoneNumber\":\"5555555555\",\"birthDate\":\"2022-11-27T19:13:57.458Z\"}",
    "env": {
        "Blob": [Function Blob
        ],
        "FormData": [Function FormData
        ]
    },
    "headers": {
        "Accept": "application/json, text/plain, */*",
        "Content-Type": "application/json"
    },
    "maxBodyLength": -1,
    "maxContentLength": -1,
    "method": "post",
    "timeout": 0,
    "transformRequest": [
        [Function transformRequest
        ]
    ],
    "transformResponse": [
        [Function transformResponse
        ]
    ],
    "transitional": {
        "clarifyTimeoutError": false,
        "forcedJSONParsing": true,
        "silentJSONParsing": true
    },
    "url": "http://10.0.2.2:5000/api/User/Register",
    "validateStatus": [Function validateStatus
    ],
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN"
}
    Request: {
    "DONE": 4,
    "HEADERS_RECEIVED": 2,
    "LOADING": 3,
    "OPENED": 1,
    "UNSENT": 0,
    "_aborted": false,
    "_cachedResponse": undefined,
    "_hasError": true,
    "_headers": {
        "accept": "application/json, text/plain, */*",
        "content-type": "application/json"
    },
    "_incrementalEvents": false,
    "_lowerCaseResponseHeaders": {},
    "_method": "POST",
    "_perfKey": "network_XMLHttpRequest_http://10.0.2.2:5000/api/User/Register",
    "_performanceLogger": {
        "_closed": false,
        "_extras": {},
        "_pointExtras": {},
        "_points": {
            "initializeCore_end": 606677771.370125,
            "initializeCore_start": 606677745.343875
        },
        "_timespans": {
            "network_XMLHttpRequest_http://10.0.2.2:5000/api/User/Register": [Object
            ],
            "network_XMLHttpRequest_http://personalIp:19000/logs": [Object
            ],
            "network_XMLHttpRequest_http://personalIp:19000/symbolicate": [Object
            ],
            "network_XMLHttpRequest_http://personalIp:5000/api/User/Register": [Object
            ],
            "network_XMLHttpRequest_http://localhost:5000/api/User/Register": [Object
            ]
        }
    },
    "_requestId": null,
    "_response": "The request timed out.",
    "_responseType": "",
    "_sent": true,
    "_subscriptions": [],
    "_timedOut": true,
    "_trackingName": "unknown",
    "_url": "http://10.0.2.2:5000/api/User/Register",
    "readyState": 4,
    "responseHeaders": undefined,
    "status": 0,
    "timeout": 0,
    "upload": {},
    "withCredentials": true
}

当前仅在移动端Expo应用中测试,Web版因SecureStore无法使用未测试,添加注册请求前移动端GET请求完全正常。

内容的提问来源于stack exchange,提问作者OCDAmmo3

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 01:31:08