Cordova下JS的fetch请求URL被自动从http转为https的问题求助
问题
首次使用Cordova,将纯HTML/CSS/JS前端+Node.js后端的项目迁移到Cordova时,登录请求出现异常:代码中明确使用http://MY_URL:8000/api/login发起POST请求,但Chrome DevTools显示实际请求的是HTTPS URL,导致报错。服务器通过Postman和原有项目均可正常访问,不想使用HTTPS,需要让Cordova允许HTTP请求。
当前config.xml配置如下:
<?xml version='1.0' encoding='utf-8'?> <widget id="my.id.here" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0"> <name>name</name> <description>desc</description> <author email="dev@cordova.apache.org" href="https://cordova.apache.org"> author </author> <content src="index.html" /> <preference name="scheme" value="http" /> <allow-navigation href="http://MY_URL/*" /> <allow-intent href="http://*/*" /> <access origin="http://MY_URL" /> <platform name="android"> <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application"> <application android:usesCleartextTraffic="true" /> </edit-config> <icon src="res/icon.png" platform="android" width="512" height="512" density="hdpi" /> <preference name="SplashScreenDelay" value="10000" /> <preference name="FadeSplashScreen" value="false"/> <preference name="SplashScreen" value="screen"/> <feature name="SplashScreen" > <param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" /> <param name="onload" value="true" /> </feature> <feature name="Vibration"> <param name="android-package" value="org.apache.cordova.vibration.Vibration" /> </feature> <splash src="res/screen/android/splash-land-ldpi.png" /> <splash qualifier="land-hdpi" src="res/screen/android/splash-land-hdpi.png" /> <splash qualifier="land-ldpi" src="res/screen/android/splash-land-ldpi.png" /> <splash qualifier="land-mdpi" src="res/screen/android/splash-land-mdpi.png" /> <splash qualifier="land-xhdpi" src="res/screen/android/splash-land-xhdpi.png" /> <splash qualifier="land-xxhdpi" src="res/screen/android/splash-land-xxhdpi.png" /> <splash qualifier="land-xxxhdpi" src="res/screen/android/splash-land-xxxhdpi.png" /> <splash qualifier="port-hdpi" src="res/screen/android/splash-port-hdpi.png" /> <splash qualifier="port-ldpi" src="res/screen/android/splash-port-ldpi.png" /> <splash qualifier="port-mdpi" src="res/screen/android/splash-port-mdpi.png" /> <splash qualifier="port-xhdpi" src="res/screen/android/splash-port-xhdpi.png" /> <splash qualifier="port-xxhdpi" src="res/screen/android/splash-port-xxhdpi.png" /> <splash qualifier="port-xxxhdpi" src="res/screen/android/splash-port-xxxhdpi.png" /> <preference name="AndroidPersistentFileLocation" value="Compatibility" /> <application name="requestLegacyExternalStorage=" value="true" /> </platform> <plugin name="cordova-plugin-media" spec="6.1.0" /> <plugin name="cordova-plugin-battery-status" spec="2.0.3" /> <plugin name="cordova-plugin-screen-orientation" spec="3.0.3" /> <plugin name="cordova-plugin-vibration" spec="3.1.1" /> <plugin name="cordova-plugin-whitelist" spec="1.3.5" /> </widget>
解决方案
针对HTTP请求被自动转为HTTPS的问题,按以下步骤排查修复:
修正
accessorigin配置
当前access标签仅允许http://MY_URL,但请求的是带端口8000的地址,需修改为包含端口的匹配规则:<access origin="http://MY_URL:8000/*" />开发环境下也可直接允许所有HTTP请求:
<access origin="http://*/*" />确认明文流量配置生效
Android 9+默认禁止明文HTTP流量,需手动检查platforms/android/app/src/main/AndroidManifest.xml,确保<application>标签包含属性:<application ... android:usesCleartextTraffic="true">如果
edit-config未生效,直接手动添加该属性。排查HTTPS强制跳转逻辑
- 检查前端代码是否有全局请求拦截器(如自定义fetch封装),是否存在自动将HTTP转为HTTPS的逻辑;
- 确认Node.js后端是否配置了HTTPS强制跳转中间件(如
express-sslify),暂时关闭该逻辑。
修复
config.xml语法错误
Android平台下存在错误配置,将:<application name="requestLegacyExternalStorage=" value="true" />修改为正确的preference标签:
<preference name="requestLegacyExternalStorage" value="true" />清理并重新构建项目
执行命令清理旧构建文件,重新生成平台代码:cordova clean android cordova build android
内容的提问来源于stack exchange,提问作者Christophe RUIZ
相关产品推荐
相关产品推荐

