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

React Native Axios请求异常:自托管JSON数据无法加载求助

Troubleshooting Axios Unhandled Promise Rejection (ID:0) in React Native

Hey there! Let's break down why your Axios request stopped working after switching to your own host URL. Here are the most likely causes and actionable fixes to try:

1. Add Error Handling to See the Real Issue

First off, your code doesn't catch failed requests, which is why you're getting that vague "unhandled promise rejection" error. Let's add a .catch() block to log specific details—this will tell you exactly what's going wrong:

state = { data: [] };

// Note: componentWillMount is deprecated, use componentDidMount instead
componentDidMount() {
  axios.get('http://ozandikbas.com/sendingdata2')
    .then(response => this.setState({ data: response.data }))
    .catch(error => {
      console.error('Request failed reason:', error.message);
      console.error('Full error details:', error); // Log full error for deeper debugging
    });
}

Once you add this, check your debug console—you'll get concrete info like "Network Error" or "CORS blocked" that narrows down the problem.

2. Fix HTTP vs HTTPS Security Restrictions

Your original working URL uses HTTPS, but your new one uses HTTP. Modern mobile platforms block unencrypted HTTP requests by default:

  • iOS: Apple's App Transport Security (ATS) blocks HTTP requests unless explicitly allowed. For debugging, open your Info.plist and add:

    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>
    

    (For production, switch your host to HTTPS instead of using this setting.)

  • Android: Add android:usesCleartextTraffic="true" to the <application> tag in your AndroidManifest.xml:

    <application
      ...
      android:usesCleartextTraffic="true">
    

3. Resolve CORS (Cross-Origin Resource Sharing) Blocks

Even if your URL works in a browser, React Native might block the request if your host doesn't send proper CORS headers. To fix this:

  • On your server, add the Access-Control-Allow-Origin header. For testing, set it to * (allow all origins), but restrict it to your app's domain in production.
  • If you can't modify the server, use a local proxy tool to forward requests, which bypasses CORS checks.

4. Verify Your Host Endpoint is Functional

Double-check that http://ozandikbas.com/sendingdata2 actually returns valid JSON:

  • Open the URL in a browser or Postman. If it doesn't load, returns a 404, or non-JSON content, that's the root issue. Fix your host's endpoint first before debugging the app.

Bonus: Use a Supported Lifecycle Method

componentWillMount is deprecated in React. Stick with componentDidMount (as shown in the code above)—it's the standard, safe place to run data-fetching logic.


内容的提问来源于stack exchange,提问作者Yağız Ozan Dikbaş

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:44:05