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

Cordova 10.1.2 API31下HTTP请求失效求助(API30时正常)

解决Cordova API级别升级到31后HTTP请求失效问题

问题原因

Android 12(对应API级别31)默认拦截所有明文HTTP请求,仅允许HTTPS通信,这是系统层面的网络安全策略变更,导致你的HTTP请求无法正常执行。

解决方案

1. 创建网络安全配置文件

在Cordova项目的res/xml目录下(若目录不存在请手动创建),新建network_security_config.xml文件,内容根据需求选择:

  • 全局允许明文HTTP请求(不推荐,仅用于测试或特殊场景):
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>
  • 仅允许指定域名的HTTP请求(推荐,更安全):
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">your-http-domain.com</domain>
    </domain-config>
</network-security-config>

替换your-http-domain.com为实际需要访问的HTTP域名。

2. 配置config.xml关联安全文件

打开项目根目录的config.xml,找到<platform name="android">节点,添加以下配置:

<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
    <application android:networkSecurityConfig="@xml/network_security_config" />
</edit-config>
<resource-file src="res/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />

3. 重新构建项目

执行以下命令清理并重新构建Android项目,确保配置生效:

cordova clean
cordova build android

额外提示

你的AJAX代码中使用了async: false同步请求,这会阻塞应用主线程,导致界面卡顿,建议移除该参数改用异步请求模式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 11:45:34