android - ionic 构建android后http请求失败

标签 android ionic-framework cordova-plugins

我提到了https://github.com/apache/cordova-plugin-whitelist并在下面添加到我的 config.xml 中:

<plugin name="cordova-plugin-whitelist" spec="~1.3.0"/>
  <access origin="*" subdomains="true"/>
  <!-- A wildcard can be used to whitelist the entire network,
     over HTTP and HTTPS.
     *NOT RECOMMENDED* -->
  <allow-navigation href="*"/>
  <allow-intent href="*"/>
  <allow-intent href="http://*/*" />
  <allow-intent href="https://*/*" />
  <allow-intent href="tel:*" />
  <allow-intent href="sms:*" />
  <allow-intent href="mailto:*" />
  <allow-intent href="geo:*" />
  <platform name="android">
      <allow-intent href="market:*" />
  </platform>

我的 AndroidManifest.xml 中也有以下权限:

<uses-permission android:name="android.permission.INTERNET" />

下面是 index.html 中的 CSP 声明:

<meta http-equiv="Content-Security-Policy" 
        content="default-src 'self' data: gap: https://ssl.gstatic.com; style-src 'self' 'unsafe-inline'; media-src *;script-src 'self' localhost:35729 84.254.133.91:8080 'unsafe-inline' 'unsafe-eval';connect-src *">

服务器运行在@84.254.133.91:8080

这是带有 http post 调用的代码:

var req = {
                method: 'POST',
                url: $rootScope.labels.IP + $rootScope.labels.USER_ID_CREATE_URL,

                data: {
                    userId: $scope.newUserIdCreate.username,
                    password: $scope.newUserIdCreate.password,

                }
            };

            var res = $http(req);
            res.success(function (data, status, headers, config) {
                alert("request success:" + JSON.stringify({ data: data }));

            });

            res.error(function (data, status, headers, config) {
                alert("request failed:" + JSON.stringify({ data: data }));

            });

$http 请求均未通过。我花了将近2天的时间,引用了'n'篇文章,仍然无法破解这个问题。

此外,我添加了一个指向“google/facebook”的 href 链接,我可以从应用程序访问这些网站。

通过运行“ionic serve”来使用网络应用程序效果非常好。在 Android apk 中,没有任何请求到达服务器。

缺少什么配置?

最佳答案

经过几天的大量调查,问题与CORS issue with Tomcat and Android Webview相同

按照上述问题的答案之一添加 CordovaOriginWrapper 解决了该问题。

这很棘手,ionic 文档从不讨论这种行为。 Ionic 文档需要根据此行为进行更新。

关于android - ionic 构建android后http请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40817095/

相关文章:

ios - Cordova-IOS 如何获取 iPhone UUID 或替代标识符

java - 无法将 jar 从 MavenLocal 导入 Android Studio - 意外的顶级异常 : finished with non-zero exit value 1

Android 切换到 gradle 不编译 JRE7 代码特性

javascript - 未显示 Angular 范围值

javascript - 带有使用摄像头的托管 Web 应用程序的 Cordova

cordova - 如何向 Cordova 或 Phonegap 添加自定义平台

android - 将android中的大文件发布到服务器

android - onClickListener 无法解析为变量

cordova - 类似 Instagram 的强制触摸弹出模式

reactjs - 在路由上重新渲染 Ionic React(不想重新加载数据)