【问题标题】:cordova-HTTP: most simple GET request not workingcordova-HTTP:最简单的 GET 请求不起作用
【发布时间】:2020-07-11 02:25:41
【问题描述】:

我正在尝试使用 cordova 插件“cordova-HTTP”从我的 Android 模拟器向运行模拟器的笔记本电脑发送 GET 请求:

cordovaHTTP.get(
            "http://10.0.2.2:80/",
            {},
            {},
            function(response) {
                console.log('success called');
            },
            function(error_response) {console.log('error called'); console.log(error_response.status);}
        );

错误回调被调用,状态码为 500(= 内部服务器错误)。但是在我的笔记本电脑上,我可以通过 tcpdump 看到甚至没有收到任何包裹。为什么这个非常简单的 GET 请求不起作用?

当我在模拟器上使用 Google Chrome 并导航到“http://10.0.2.2:80/”时一切正常,我看到了安装在笔记本电脑上的 Apache2 网络服务器的虚拟页面。

【问题讨论】:

    标签: cordova http android-emulator cordova-plugins http-get


    【解决方案1】:

    由于 API 级别 28,默认情况下不允许发送简单的 GET 请求,因为不允许明文流量。所以我在我的cordova项目的config.xml中添加了以下内容(在<platform name="android">标签内):

    <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application"> 
    <application android:usesCleartextTraffic="true" /> 
    </edit-config>
    

    还要确保在 &lt;widget&gt; 标签内添加 android XML 命名空间,否则上述方法将不起作用:

    xmlns:android="http://schemas.android.com/apk/res/android"
    

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2016-04-24
      相关资源
      最近更新 更多