科尔多瓦/离子:在模拟或在设备上运行$ http请求不处理 [英] Cordova / Ionic : $http request not processing while emulating or running on device

查看:119
本文介绍了科尔多瓦/离子:在模拟或在设备上运行$ http请求不处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切上周顺利,而我是对运行设备的应用程序或Genymotion模仿,一切以API的调用进行工作(无论返回的数据或失败,但至少显示的东西)。

Everything were going well last week and while i was running the application on device or emulating with Genymotion, all the calls to the api were working (Either returning the data or failing but at least showing something).

我用

ionic run android

我添加到更新全局科尔多瓦离子:

I add to update the global cordova ionic:

npm install -g cordova ionic

由于所有的$ HTTP请求甚至不处理。我不能得到任何回应,而API是仍在工作的罚款和CORS完美设置。

Since that all the $http requests are not even processing. I can't get any responses while the Api is still working fine and the CORS are perfectly set.

我发现的唯一方法是使用选项--livereload或-l:

The only way i found is to use the option --livereload or -l :

ionic run -l android

我要避免使用livereload不惜任何代价。

I want to avoid using the livereload at any cost.

我开始使用离子1.0.0和科尔多瓦LIB 4.3.0从头开始创建一个项目。

I started to create a project from scratch using ionic 1.0.0 and cordova lib 4.3.0.

angular.module('starter.controllers', [])

.controller('AppCtrl', function($scope, $ionicModal, $timeout, $http) {

  alert('calling api');
  // Create an anonymous access_token
  $http
      .get(domain+'/oauth/v2/token?client_id='+public_id+'&client_secret='+secret+'&grant_type=client_credentials')
      .then(function(response){
          alert(response.data.access_token);
      });
})

因此​​,虽然使用:

So while using :

ionic serve

这是正确的提醒'调用API则响应(对于例如OAuth访问令牌)。

It is correctly alerting 'calling api' then the response (An OAuth access token for that example).

不过,虽然使用:

ionic run android

它只是提醒'调用API,但似乎并没有处理HTTP请求。​​

It is only alerting 'calling api' but doesn't seem to process the http request.

有没有人遇到类似的东西吗?我得到上大难题。

Did anyone experience something similar? I'm getting big headaches on that.

推荐答案

通过科尔多瓦4.0.0的更新,你将面临不能够进行HTTP调用REST风格的API和加载外部资源,包括其他的问题HTMLS /视频/音频/图像。

With the update of Cordova 4.0.0, you will face an issue of not being able to make HTTP calls to RESTful APIs and load external resources, which include other HTMLs/video/audio/images.

使用白名单科尔多瓦 - 插件白名单域解决了这个问题。

Whitelisting the domains using cordova-plugin-whitelist solves the issue.

通过CLI添加插件:

cordova plugin add cordova-plugin-whitelist

然后code的下面一行添加到您的应用程序的 config.xml中它位于应用程序的根目录:

and then add the following line of code to your app's config.xml which is located in your application's root directory:

Reccomended 的文件中:

<allow-navigation href="http://example.com/*" />

<allow-navigation href="http://*/*" />

标记中的 index.html的

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

原因针对此问题:

The reason for this issue:

从科尔多瓦4.0.0用于Android的更新:

From Cordova 4.0.0 for Android's update:

白名单功能改组


      
  • 您将需要添加新的科尔多瓦 - 插件白名单插件来继续使用白名单

  • You will need to add the new cordova-plugin-whitelist plugin to continue using a whitelist

设置内容安全性策略(CSP),现在支持,并加入白名单(见插件自述详细信息)的推荐方式

Setting a Content-Security-Policy (CSP) is now supported and is the recommended way to whitelist (see details in plugin readme)

网络请求被阻止在默认情况下没有插件,所以即使安装这个插件,允许所有的请求,即使你是
  使用CSP。

Network requests are blocked by default without the plugin, so install this plugin even to allow all requests, and even if you are using CSP.

这新的白名单增强,更加安全和可配置的,但传统的白名单的行为通过单独的仍然是可用
  插件(不推荐)。

This new whitelist is enhanced to be more secure and configurable, but the Legacy whitelist behaviour is still available via a separate plugin (not recommended).

请注意:在此版本中没有严格的一部分,最新的默认应用
  通过科尔多瓦-CLI创建将包含此插件默认情况下。

Note: while not strictly part of this release, the latest default app created by cordova-cli will include this plugin by default.

这篇关于科尔多瓦/离子:在模拟或在设备上运行$ http请求不处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆