Phonegap - проблема вызова внешнего PHP - PullRequest
0 голосов
/ 27 сентября 2018

Я создаю приложение Phonegap (cli-8.0.0, ios-4.5.4, android-7.0.0), которое должно пропинговать внешние PHP-скрипты, размещенные на случайном сервере.

Когда ятестирование с помощью приложения Phonegap Developer - Успех.

Когда я тестирую после установки APK из Phonegap Build - Ошибка

Я установил 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 произвольны.

Есть идеи?

Ответы [ 2 ]

0 голосов
/ 01 октября 2018

ОК, поэтому проблема в моей Политике безопасности контента в моем index.html в строке 4.

Я пренебрег разрешением любых других доменов взаимодействовать сприложение. 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:;">
0 голосов
/ 27 сентября 2018

Вы просто заменяете свой код на эти.

Вы забыли установить .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();
}

};

...