首页 文章

Phonegap - 调用外部PHP的问题

提问于
浏览
0

我正在创建一个需要ping随机服务器上托管的外部PHP脚本的Phonegap应用程序(cli-8.0.0,ios-4.5.4,android-7.0.0) .

当我使用Phonegap Developer App测试时 - 成功 .

当我从Phonegap Build - Error安装APK后测试

我已经安装了 cordova-plugin-whitelist 和我'm waiting for '设备就绪......' .

这是我的 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

    好的,问题是我在 index.html 第4行的 Content Security Policy .

    我忽略了允许任何其他域与应用程序通信 . 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

    您只需用这些替换您的代码即可 .

    你忘了设置.bind(这个)

    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();
    }
    

    };

相关问题