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

Windows本地React Native连接Node.js API返回undefined问题

问题排查与解决方案

1. 修复Axios响应拦截器的核心问题

你的Axios响应拦截器的成功回调未返回response,导致后续then方法无法获取到响应数据,直接得到undefined。修改拦截器代码:

axiosInstance.interceptors.response.use(
  function (response) {
    // 必须返回response,否则后续then无法拿到数据
    return response;
  },
  function (error) {
    EncryptedStorage.removeItem('accessToken');
    EncryptedStorage.removeItem('user');
    axiosInstance.defaults.headers.common['Authorization'] = '';
    return Promise.reject(error);
  },
);

2. 其他可能的排查方向

如果修复拦截器后问题仍存在,检查以下内容:

  • Windows防火墙设置:确保8000端口允许Node.js应用的入站连接,避免防火墙阻断React Native的请求。
  • Android网络权限:在AndroidManifest.xml中确认添加网络权限:
    <uses-permission android:name="android.permission.INTERNET" />
    
  • iOS ATS配置:若使用iOS模拟器,在Info.plist中添加配置允许HTTP请求(你的API使用http协议):
    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>
    
  • 网络一致性:确认React Native运行的设备/模拟器和Node.js服务器处于同一WiFi网络,且IPv4地址192.168.1.5准确无误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 10:16:04