错误未能在“XMLHttpRequest”上执行“open”:拒绝连接“http://192.168.XX.XXX:8080/TestApp/addRole/”:Ionic

use*_*589 5 angularjs ionic-framework

在ionic中调用rest api时遇到错误。如果我在浏览器 firefox 或 chrome 上运行,那么它工作正常..但是当我在 android 设备上运行时,它会出现以下错误:

Error: Failed to execute 'open' on 'XMLHttpRequest': Refused to connectto 'http://192.168.XX.XXX:8080/TestApp/addRole/' becuase it violates the document's content security Policy
at Error (native( at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:23357:16
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题

如果我在 index.html 中设置以下元标记如下,然后在 chrome 和 android 设备中出错..不在 firefox 中

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
Run Code Online (Sandbox Code Playgroud)

在我的休息服务中,我将标题设置如下

responseHeaders.add("Access-Control-Allow-Origin", "*"); responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");

我也安装了白名单插件但没有运气

我该如何解决这个问题?请帮忙

the*_*pio 3

在 Android 上,您需要在 @index.html 中设置 content-security-policy 元标记<head>

它看起来像这样:

<meta http-equiv="Content-Security-Policy" content="default-src * 'self' 'unsafe-inline' data: gap: 'unsafe-eval'; style-src * 'self' 'unsafe-inline'; connect-src * ; script-src * 'self' 'unsafe-inline'; media-src *">
Run Code Online (Sandbox Code Playgroud)

在这种情况下,有很多不安全和不必要的标签,它们几乎允许任何内容,但您可以在此处阅读有关它们的更多信息,例如:

Cordova 白名单插件内容安全策略

在同一页面中还有其他内容的说明,例如网络请求白名单。

希望这可以帮助您解决问题。