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

React Native应用调用Node.js后端Axios GET请求遇网络错误求方案

Troubleshooting Axios Network Error in React Native with Node.js Backend

Hey there, let’s work through this network error step by step—I’ve dealt with this exact scenario multiple times, so here are some targeted fixes you might not have tried yet:

1. Replace 127.0.0.1 with Your Computer’s Local Network IP

The biggest gotcha here is that 127.0.0.1 in React Native points to the emulator/physical device itself, not your Node.js server running on your computer. To fix this:

  • Windows: Open Command Prompt and run ipconfig, find the IPv4 address under your active WiFi/Ethernet adapter.
  • Mac/Linux: Open Terminal and run ifconfig (or ip addr for newer Linux distros) to get your local IP.
  • Update your Axios request to use this IP:
    const response = await axios.get('http://192.168.x.x:8000/topratedcars'); // Replace with your actual IP
    
  • Make sure your mobile device/emulator is connected to the same WiFi network as your computer.

2. Fix CORS Restrictions on Your Node.js Backend

Even if your IP is correct, Cross-Origin Resource Sharing (CORS) might block the request from React Native. Here’s how to resolve it:

  • Install the cors npm package (simplest for development):
    npm install cors
    
  • Add it to your Node.js server setup:
    const cors = require('cors');
    const express = require('express');
    const app = express();
    
    app.use(cors()); // Allows all origins (safe for development only)
    
  • If you don’t want to use the package, manually set CORS headers:
    app.use((req, res, next) => {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
      res.header('Access-Control-Allow-Headers', 'Content-Type');
      next();
    });
    

3. Verify Your Backend is Reachable First

Before debugging React Native, confirm your Node.js server is working properly:

  • Open your computer’s browser and visit http://127.0.0.1:8000/topratedcars. If you don’t get a valid response, fix your backend first:
    • Check if the server is running (look for terminal logs confirming it’s listening on port 8000).
    • Ensure the /topratedcars route is defined correctly in your backend code.
    • Make sure no other program is using port 8000 (use lsof -i :8000 on Mac/Linux or netstat -ano | findstr :8000 on Windows to check).

4. Configure Network Permissions for Android/iOS

React Native apps need explicit permissions to access the network, and iOS blocks unencrypted HTTP requests by default:

  • Android: Open android/app/src/main/AndroidManifest.xml and add these permissions if missing:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    
  • iOS: Open ios/YourAppName/Info.plist and add an exception for HTTP requests (for development only—use HTTPS in production):
    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>
    

5. Tweak Axios Request Configuration

Sometimes adjusting Axios settings can resolve subtle network issues:

  • Add a timeout to avoid hanging requests:
    const response = await axios.get('http://your-local-ip:8000/topratedcars', {
      timeout: 10000, // 10-second timeout
      headers: {
        'Content-Type': 'application/json',
      },
    });
    

6. Check Firewall/Proxy Interferences

  • Temporarily disable your computer’s firewall to see if it’s blocking incoming requests on port 8000.
  • If you’re using a VPN or corporate proxy, disconnect it—these can often intercept or block local network traffic.

Start with the first two fixes (IP address and CORS) since those are the most common culprits. Let me know if any of these resolve your issue!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 10:22:40