我正在创建一个Phonegap应用(cli-8.0.0,ios-4.5.4,android-7.0.0),该应用需要ping随机服务器上托管的外部PHP脚本。
当我使用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都是任意的。
有什么想法吗?
答案 0 :(得分:0)
您只需用这些代码替换代码即可。
您忘记设置.bind(this)
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
this.receivedEvent();
},
// Update DOM on a Received Event
receivedEvent: 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();
}
};
答案 1 :(得分:0)
好,所以问题出在第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:;">