gpt4 book ai didi

angularjs - Cordova / ionic : $http request not processing while emulating or running on device

转载 作者:行者123 更新时间:2023-12-04 02:40:36 32 4
gpt4 key购买 nike

上周一切顺利,当我在设备上运行应用程序或使用 Genymotion 进行模拟时,对 api 的所有调用都在工作(返回数据或失败,但至少显示了一些东西)。

我正在使用

ionic run android

我添加更新全局cordova ionic:
npm install -g cordova ionic

因为所有 $http 请求甚至都没有处理。当 Api 仍然正常工作并且 CORS 设置完美时,我无法得到任何响应。

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

我想不惜一切代价避免使用 livereload。

我开始使用 ionic 1.0.0 和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);
});
})

所以在使用时:
ionic serve

它正确地警告“调用 api”,然后是响应(该示例的 OAuth 访问 token )。

但是在使用时:
ionic run android

它只是提醒“正在调用 api”,但似乎没有处理 http 请求。

有没有人经历过类似的事情?我对此很头疼。

最佳答案

随着 Cordova 4.0.0 的更新,您将面临无法对 RESTful API 进行 HTTP 调用和加载外部资源(包括其他 HTML/视频/音频/图像)的问题。
使用 cordova-plugin-whitelist 将域列入白名单解决了这个问题。
如果已安装,请删除白名单插件:

cordova plugin remove cordova-plugin-whitelist
通过 CLI 添加白名单插件:
cordova plugin add cordova-plugin-whitelist
然后将以下代码行添加到您的应用程序的 config.xml 它位于您的应用程序的根目录中:
推荐 在文档中:
<allow-navigation href="http://example.com/*" />
或:
<allow-navigation href="http://*/*" />

meta在您的 index.html 中添加标签
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"/>
出现这个问题的原因:
从 Cordova 4.0.0 for Android 的更新:

Whitelist functionality is revamped

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

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

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

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

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

关于angularjs - Cordova / ionic : $http request not processing while emulating or running on device,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30326148/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com