【问题标题】:$http requests failing on Ionic after 'ionic build android'在“ionic build android”之后,$http 请求在 Ionic 上失败
【发布时间】:2015-08-11 08:40:08
【问题描述】:

我在开发过程中一直在使用 ionic serve 测试我的应用程序,一切正常。我现在正在尝试使用 ionic build android 导出 apk,然后将文件“android-debug.apk”存储在我的网络服务器上,并通过手机的浏览器下载并安装它以在现实世界中进行测试。

应用程序安装正常,但不允许我发出任何 $http 请求。我已经安装了插件 cordova-plugin-whitelist,并且还在我的 config.xml 文件中添加了以下内容:

    <plugin name="cordova-plugin-whitelist" spec="1" />
      <access origin="*" />
<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>
      <platform name="ios">
          <allow-intent href="itms:*" />
          <allow-intent href="itms-apps:*" />
      </platform>

除此之外,我已将其添加到我的主 index.html 文件中:

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

谁能告诉我为什么请求仍然失败?

我已经在 2 台不同的服务器上进行了测试,都启用了 CORS,并且已经使用 Postman 进行了验证。

感谢您的帮助

编辑:

我的 $http 请求如下所示:

$http.get('http://url.com/request').then(function(res) {
                    console.log('success');
                    console.log(res);
                }, function(res) {
                    console.log('error');
                    console.log(res);
                });

启用远程 chrome 调试后,我收到一个错误警报,后面跟着一个对象:

data: null,
status: 0,
statusText: ""

【问题讨论】:

  • 您可以使用 chrome 检查混合应用程序,它可以向您显示错误。将您的手机连接到您的系统并检查应用程序。
  • 我刚试过,我得到的错误是:POST: http//url.com net::ERR_CACHE_MISS...虽然没有提供更多信息
  • 在网络选项卡中,请求显示(failed) xhr
  • 复制 url 并尝试通过系统中的邮递员运行它。看看你能不能得到回应。

标签: android angularjs cordova ionic-framework ionic


【解决方案1】:

看来我需要补充:

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

进入平台/android/AndroidManifest.xml。现在工作正常!

【讨论】:

  • 我在 AndroidManifest.xml 中有权限,但仍然失败。还有其他指针吗?已经 2 天了,但仍然无法解决问题
  • 这发生在我升级(/通常弄乱)cordova 版本之后 - 也许该声明是在 Cordova 7 中添加的?执行“cordova platform remove android”然后“cordova platform add android”为我正确修复了它......
猜你喜欢
  • 1970-01-01
  • 2018-03-19
  • 2019-08-08
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
相关资源
最近更新 更多