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

XMLHTTPRequest在Cordova测试应用中无法工作,求解决方案

Troubleshooting Your Cordova Test App Issue

Hey there! Let’s dive into what might be causing your Cordova test app to fail—your hunch about not running a local server could absolutely be a key factor here. Here are some targeted fixes to work through:

1. Fill the Local Server Gap

Cordova needs a way to load your app’s web assets, and without a server, it might struggle to access local files properly. Try this first:

  • Run cordova serve in your project directory. This spins up a local server at http://localhost:8000 by default.
  • Check if you can access this URL in your browser—if your index.html and other assets load correctly here, the issue is likely tied to how Cordova tries to load resources without a server.
  • Temporarily update your config.xml’s <content> tag to point to the server: <content src="http://localhost:8000" />, then rebuild and simulate again to test.

2. Refine Your Whitelist Configuration

You tried the whitelist plugin, but let’s make sure all necessary settings are locked in:

  • Alongside <allow-navigation href="*" />, add <access origin="*" /> to your config.xml—this controls access to external network resources, which is often required even for local assets in some scenarios.
  • Reinstall the plugin with fresh commands to rule out corruption:
    cordova plugin remove cordova-plugin-whitelist
    cordova plugin add cordova-plugin-whitelist
    
  • Run cordova clean followed by cordova build [your-platform] (replace with android or ios) to clear cached build files that might be ignoring your config changes.

3. Check Platform-Specific Permissions & Settings

Different platforms have extra requirements to ensure asset loading works:

  • Android: Open platforms/android/app/src/main/AndroidManifest.xml and confirm you have the internet permission:
    <uses-permission android:name="android.permission.INTERNET" />
    
  • iOS: Open platforms/ios/[YourAppName]/Info.plist and add App Transport Security (ATS) settings to allow unencrypted loads (for testing only—adjust for production):
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    

4. Test with a Minimal Project

To rule out project-specific bugs, create a fresh Cordova app and test your setup:

cordova create testApp com.example.test TestApp
cd testApp
cordova plugin add cordova-plugin-whitelist
# Add your config.xml whitelist settings here
cordova platform add android # or ios
cordova simulate

If this minimal app works, the issue is likely in your original project’s file structure or conflicting plugins.

Your thought about the missing server is right on target—Cordova relies on loading web assets either via a local server (simulated or physical) or properly configured local file access. Starting with cordova serve should help you confirm if that’s the root cause.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:21:47