android - Phonegap - 调用外部 PHP 的问题
问题描述
我正在创建一个需要 ping 托管在随机服务器上的外部 PHP 脚本的 Phonegap 应用程序(cli-8.0.0、ios-4.5.4、android-7.0.0)。
当我使用 Phonegap Developer App 进行测试时 - 成功。
当我从 Phonegap Build 安装 APK 后进行测试时 - 错误
我已经安装了cordova-plugin-whitelist,我正在等待'DEVICE READY...'。
这是我的config.xml:
<?xml version='1.0' encoding='utf-8'?>
<widget id="xx.xx.xx.xx" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>xxxxx</name>
<description>xxxxx</description>
<author email="xxxx" href="xxxx">xxxx</author>
<preference name="phonegap-version" value="cli-8.0.0" />
<preference name="orientation" value="portrait" />
<preference name="SplashScreen" value="none" />
<content src="index.html" />
<access origin="*" />
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
<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:*" />
<resource-file src="google-services.json" target="app/google-services.json" />\
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
<resource-file src="GoogleService-Info.plist" />
</platform>
<plugin name="phonegap-plugin-push" spec="~2.1.2">
<variable name="FCM_VERSION" value="11.0.1" />
</plugin>
<engine name="android" spec="~7.0.0" />
<engine name="ios" spec="~4.5.4" />
<engine name="browser" spec="~5.0.4" />
<plugin name="cordova-plugin-file" spec="^6.0.1" />
<plugin name="cordova-plugin-network-information" spec="^2.0.1" />
<plugin name="cordova-plugin-whitelist" spec="^1.3.3" />
</widget>
这是我的index.html:
<!DOCTYPE html>
<html>
<head>
<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 *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title></title>
</head>
<body>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
这是我的index.js:
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
console.log('DEVICE READY...');
var url = "https://www.xxx.xxx.com/xxx.php?id=" + encodeURIComponent('xxx') + "&title=" + encodeURIComponent('xxx');
var xhr = new XMLHttpRequest();
xhr.onload = function() {console.log('success');}
xhr.onerror = function() {console.log('error');}
xhr.open('POST', url, true);
xhr.send();
}
};
app.initialize();
显然,所有的 XXX 都是任意的。
有任何想法吗?
解决方案
好的,问题是我在第 4 行的index.html中的内容安全策略。
我忽略了允许任何其他域与应用程序通信。default-src需要包含允许的域,否则将不允许任何域。
现在我使用了 * 通配符,它可以工作。
<meta http-equiv="Content-Security-Policy" content="default-src * data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
推荐阅读
- python - 我正在搜索但是在打印时为什么第二个输入也进来了
- python - 数据框将行中的值转换为唯一列
- python - 有没有办法在在 python 中使用硒?
- javascript - pure-react-carousel:所有幻灯片可见
- python - 如何在数据库中的每个条目上运行刮板?
- prolog - 在 Prolog 中使用 DCG 时,有没有一种方法可以递归调用非终结符号?
- c++ - .pop() 在空堆栈上的函数
- google-chrome - 响应未出现在 chrome 开发人员工具中,但动态表单数据正在为下一个请求发布
- unity3d - 如何在Unity中自动生成AR平面边缘的碰撞自动创建给定高度的墙壁?
- puppeteer-sharp - 如何使用 Puppeteer Sharp 下载文件?